CodeIgniter Forums
Facebook-style date/time display helper? (RESOLVED) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Facebook-style date/time display helper? (RESOLVED) (/showthread.php?tid=21364)



Facebook-style date/time display helper? (RESOLVED) - El Forum - 08-07-2009

[eluser]IsaacS[/eluser]
Hi everyone,

Does anyone know of an extension to the Date helper in CodeIgniter that allows for Facebook style dates? eg:

Just now/a moment ago
1 hour ago
2 days ago
5 months ago

It would be even lovelier if it was customisable, and utterly cliché-producing if it could do things like fortnights Smile

I've had a look on Google, but there's only lots of rubbish that comes up, which makes finding this helper a right pain.

I'm hoping that the Date helper has this built in, and I just haven't noticed?

Thanks!

Isaac


Facebook-style date/time display helper? (RESOLVED) - El Forum - 08-07-2009

[eluser]Pascal Kriete[/eluser]
The timespan function in the date helper should get pretty close. You can always split the string at the commas if you don't want the whole thing.

It doesn't do fortnights, but those could be added without too much effort - the code is pretty straightforward. Start with the largest one and work your way down.


Facebook-style date/time display helper? (RESOLVED) - El Forum - 08-07-2009

[eluser]IsaacS[/eluser]
OK, I'll give it a go. Thanks,

Isaac


Facebook-style date/time display helper? (RESOLVED) - El Forum - 08-07-2009

[eluser]helmutbjorg[/eluser]
This uses the standard language files too... I'm sure you could modify it to say 'a moment ago' if below a minute or so..

MY_date_helper.php
Code:
/**
* Difference
*
* Returns a difference between the specified timestamp and
* now in one of these formats:
*    10 days
*     2 years
*     46 seconds
*
* @access    public
* @param    integer    a number of seconds
* @param    integer    Unix timestamp
* @return    integer
*/    

if(!function_exists('difference')) {
    function difference($seconds='', $time='') {
        if(!is_numeric($seconds) || empty($seconds)) return true;
        $CI =& get_instance();
        $CI->lang->load('date');    
        if(!is_numeric($time)) $time = date('U');
        $difference = abs($time-$seconds);
        $periods = array('date_second', 'date_minute', 'date_hour', 'date_day', 'date_week', 'date_month', 'date_year');
        $lengths = array('60','60','24','7','4.35','12','10');
        for($j=0; $difference >= $lengths[$j]; $j++) {
            if($j==count($lengths)-1) break;
            $difference /= $lengths[$j];
        }
        $difference = round($difference);
        if($difference == 0 && $j==0) $difference = 1;
        if($difference != 1) $periods[$j].= 's';
        return $difference.' '.strtolower($CI->lang->line($periods[$j]));
    }
}

Usage
Code:
echo difference('21546215621'); // Outputs: 2 minutes

// You could add the ago yourself
echo difference('21546215621').' ago';



Facebook-style date/time display helper? (RESOLVED) - El Forum - 08-08-2009

[eluser]Johan André[/eluser]
I did this a while ago:

http://ellislab.com/forums/viewthread/106557/

Not the best, but works...


Facebook-style date/time display helper? (RESOLVED) - El Forum - 08-08-2009

[eluser]alxjvr[/eluser]
i use the ff. as a helper function, not quite elegant, but it works for my needs as well.

Code:
function days_ago($time = 1, $post = false, $lang) {
    $parts = '';
    $now = time();

    $nowparts = date_parse(date("Y-m-d h:i:s a",$now));
    $oldparts = date_parse(date("Y-m-d h:i:s a",$time));

    $elapsed = $now - $time;
    if ($elapsed < 0)
        return '<strong>Just now</strong>';
    
    $days = floor($elapsed / 86400);
    $hours = floor($elapsed / 3600);
    $minutes = floor($elapsed / 60);
    // $parts = '(d='.$days . ' h=' . $hours . ' m=' . $minutes.') ';
    $minutes -= ($hours * 60);
    $hours += ($minutes > 60) ? 1 : 0;
    $days += ($hours > 24) ? 1 : 0;
    // $parts .= '(d='.$days . ' h=' . $hours . ' m=' . $minutes.') ';

    if ($days < 3) {
        $daygap = $nowparts['day'] - $oldparts['day'];
        if ($daygap == 0) {
            if ($hours > 0)
                $h = $hours.' '.$lang->line('date_hour').($hours > 1 ? 's':'');
            else
                $h = '';
            if ($minutes > 0)
                $m = $minutes.' '.$lang->line('date_minute').($minutes > 1 ? 's':'');
            else
                $m = '';
            if ($h.$m == '')
                return $lang->line('justnow');
            else
                return $h.' '.$m.' '.$lang->line('ago');
        }
        if ($daygap == 1) {
            if ($post)
                return $lang->line('attime').' '.date('g:ia',$time) . ' '.$lang->line('yesterday');
            else
                return $lang->line('yesterday').' '.$lang->line('attime').' '.date('g:ia',$time);
        }
    }
    
    if ($days >= 2) {
        if ($post)
            return $lang->line('attime').' '.date('g:ia',$time).date(' F j',$time);
        else
            return date('F j',$time).' '.$lang->line('attime').' '.date('g:ia',$time);
    }
        
    return '';    
}



Facebook-style date/time display helper? (RESOLVED) - El Forum - 08-08-2009

[eluser]IsaacS[/eluser]
Thanks alxjvr, but I'm using a slightly modified version of Johan André's script in the link above, so this thread is now resolved.

Thanks again to all of you who've helped out on this - makes me love CI even more seeing how helpful everyone is!

Isaac