Welcome Guest, Not a member yet? Register   Sign In
date helper : date_when() - get time elapsed/to go
#1

[eluser]ntheorist[/eluser]
I was looking for a helper function to give me a date span in natural english, ie '3 days ago', '2 months from now' etc, but i decided just to write one and add it to CI's date helper. It's working pretty well for me so I thought i'd share it. It uses the date_helper now() function so its best to add it to the helper file.

it takes 1 argment, a unix timestamp, with an optional 2nd option to specify a different 'now' time:

Code:
$time = now() - 4*24*3600;
echo date_when($time); // Returns '4 days ago';

$time = now() - 1*24*3600;
echo date_when($time); // Returns 'Yesterday, 4:46pm' (or whenever the time was)

// Can do future times
$time = now() + 80*24*3600;
echo date_when($time); // Returns '2 Months from now'

// Using datetime value ie MySQL
echo date_when(human_to_unix($db_value));

// Using base date, ie Timezone change. Sort of redundant since now() does this
$actual_time = now() + 3600;
echo date_when($time, $actual_time);

Here it is:

Code:
if ( ! function_exists('date_when') )
{
    function date_when( $timestamp = NULL, $base = NULL)
    {
        if( strlen($timestamp) < 10 ) return;
        
        if( empty($base) ) $base = now();
        
        // Is timestamp in past or future?
        $past = ($base > $timestamp) ? TRUE : FALSE;
        
        // Create suffix based on past/future
        $suffix = $end = ($past) ? ' ago' : ' from now';
        
        // Actual time string of timestamp ie 4:54 pm
        $timestr = date('g:i a',$timestamp);
        
        $diff = abs($timestamp - $base);
        
        $periods = array('year'=>31536000,'month'=>2628000,'day'=>86400,'hour'=>3600,'minute'=>60,'second'=>1);
        
        // create array holding count of each period
        
        $out = array();
        
        foreach($periods as $period => $seconds)
        {
            if( $diff > $seconds )
            {
                $result = floor($diff/$seconds);
                $diff =  $diff % $seconds;
                $out[] = array($period, $result);
            }
        }
        
        // Get largest period, other counts are still in $out for use
        $top = array_shift($out);
        
        switch($top[0])
        {
            case 'month' :
                $output = $top[1] == 1 ? ( $past ? 'last month' : 'next month' ) : $top[1] . ' months' . $suffix;
                break;
            case 'day' :
                $output = $top[1] == 1 ? ( $past ? 'yesterday' : 'tomorrow' ) .', '. $timestr : $top[1] . ' days' . $suffix;
                break;
            case 'hour':
                // Calculate in case, for example if yesterday was only 7 hours ago
                $output = date('j',$base) == date('j',$timestamp) ? 'today, '.$timestr : (( $past ? 'yesterday' : 'tomorrow' ) . ', '.$timestr);
                break;
            default :
                $output = $top[1] .' '. $top[0] . ( $top[1] > 1 ? 's' : '' ) . $suffix;
                break;
        }
        
        
        return ucfirst($output);
        
    }
}

It's not 100% accurate. As with most date stuff it could be improved to include exact number of seconds in current month, account for leap years etc. But since it returns only the highest span of time, only in very narrow circumstances it would output, for instance 'Last Month' where it should output '2 months ago', etc.

cheers,

_CC


Messages In This Thread
date helper : date_when() - get time elapsed/to go - by El Forum - 06-30-2009, 10:40 PM
date helper : date_when() - get time elapsed/to go - by El Forum - 08-14-2009, 07:35 PM
date helper : date_when() - get time elapsed/to go - by El Forum - 08-15-2009, 03:25 AM
date helper : date_when() - get time elapsed/to go - by El Forum - 10-15-2010, 09:10 AM
date helper : date_when() - get time elapsed/to go - by El Forum - 10-15-2010, 11:44 AM
date helper : date_when() - get time elapsed/to go - by El Forum - 10-15-2010, 02:41 PM



Theme © iAndrew 2016 - Forum software by © MyBB