Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_View_Helper_TimeAgo Class Reference

Format a passed date/time in a friendlier manner such as: just now 1 minute ago 3 weeks ago etc. More...

List of all members.

Public Member Functions

 timeAgo ($dateTime, $fallbackFormat= 'F d, Y g:i a')
 Takes the passed dateTime and converts it to a friendlier format such as: just now 1 minute ago 3 weeks ago etc.

Detailed Description

Format a passed date/time in a friendlier manner such as: just now 1 minute ago 3 weeks ago etc.

January 2, 2010 5:23 pm

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

P4Cms_View_Helper_TimeAgo::timeAgo ( dateTime,
fallbackFormat = 'F d,
Y g:i a'   
)

Takes the passed dateTime and converts it to a friendlier format such as: just now 1 minute ago 3 weeks ago etc.

We assume a month is 28 days. For values older than 12 of our months (336 days) the fallback format will be utilized with date to generate the output. The default format returns the style: January 4, 2010 4:32 pm

Parameters:
string | int$dateTimethe value to format; strings must be strtotime compatible
string$fallbackFormatoptional - date format to use for values older than 336 days (12 psuedo months)
Returns:
string the truncated string.
                                                                  :i a')
    {
        // cast purely numeric strings to int
        if (is_string($dateTime) && $dateTime === (string)(int)$dateTime) {
            $dateTime = (int)$dateTime;
        }

        if (is_string($dateTime)) {
            $dateTime = strtotime($dateTime);
        }

        if (!is_int($dateTime)) {
            throw new InvalidArgumentException(
                'Expected int or strtotime compatible string'
            );
        }

        // entry 1 is max age in seconds that rule applies to
        // entry 2 is divisor to apply to value
        // entry 3 is text to append to divided value; text
        //         alone is used when entry 2 is 0
        $times = array(
            array(60,        0,          'just now'),
            array(120,       0,          '1 minute ago'),
            array(3600,      60,         'minutes ago'),
            array(7200,      0,          '1 hour ago'),
            array(86400,     3600,       'hours ago'),
            array(172800,    0,          'yesterday'),
            array(604800,    86400,      'days ago'),
            array(1209600,   0,          'last week'),
            array(2419200,   604800,     'weeks ago'),
            array(4838400,   0,          'last month'),
            array(29030400,  2419200,    'months ago')
        );

        $seconds = time() - $dateTime;
        foreach ($times as $time) {
            if ($seconds < $time[0]) {
                if ($time[1]) {
                    return floor($seconds / $time[1]) . " " . $time[2];
                }

                return $time[2];
            }
        }

        // if we made it here; return a formatted
        // date as entry is older than 336 days
        return date($fallbackFormat, $dateTime);
    }

The documentation for this class was generated from the following file: