tags:

views:

7276

answers:

4

Are there any classes/functions written in php publicly available that will take a timestamp, and return the time passed since then in number of days, months, years etc? Basically i want the same function that generates the time-since-posted presented together with each entry on this site (and on digg and loads of other sites).

+1  A: 

Could you just translate the C# code that SO uses?

A. Rex
+3  A: 

This is written as a wordpress plugin but you can extract the relevant PHP code no problem: Fuzzy date-time

alxp
+1  A: 

I'm not sure there will be classes for that but I've found on Google a couple of methods to achieve what you want:

Maybe one of them fits or needs or you can easily adapt it.

Nazgulled
+2  A: 

Here is a Zend Framework ViewHelper I wrote to do this, you could easily modify this to not use the ZF specific code:

/**
 * @category    View_Helper
 * @package     Custom_View_Helper
 * @author      Chris Jones <[email protected]>
 * @license     New BSD License
 */
class Custom_View_Helper_HumaneDate extends Zend_View_Helper_Abstract
{
    /**
     * Various time formats
     */
    private static $_time_formats = array(
        array(60, 'just now'),
        array(90, '1 minute'),                  // 60*1.5
        array(3600, 'minutes', 60),             // 60*60, 60
        array(5400, '1 hour'),                  // 60*60*1.5
        array(86400, 'hours', 3600),            // 60*60*24, 60*60
        array(129600, '1 day'),                 // 60*60*24*1.5
        array(604800, 'days', 86400),           // 60*60*24*7, 60*60*24
        array(907200, '1 week'),                // 60*60*24*7*1.5
        array(2628000, 'weeks', 604800),        // 60*60*24*(365/12), 60*60*24*7
        array(3942000, '1 month'),              // 60*60*24*(365/12)*1.5
        array(31536000, 'months', 2628000),     // 60*60*24*365, 60*60*24*(365/12)
        array(47304000, '1 year'),              // 60*60*24*365*1.5
        array(3153600000, 'years', 31536000),   // 60*60*24*365*100, 60*60*24*365
    );

    /**
     * Convert date into a pretty 'human' form
     *      Now with microformats!
     *
     * @param string|Zend_Date $date_from Date to convert
     * @return string
     */
    public function humaneDate($date_from)
    {
        $date_to = new Zend_Date(null, Zend_Date::ISO_8601);

        if (!($date_from instanceof Zend_Date)) {
            $date_from = new Zend_Date($date_from, Zend_Date::ISO_8601);
        }

        $dateTo     = $date_to->getTimestamp();   // UnixTimestamp
        $dateFrom   = $date_from->getTimestamp(); // UnixTimestamp
        $difference = $dateTo - $dateFrom;
        $message    = '';

        if ($dateFrom <= 0) {

            $message = 'a long time ago';

        } else {

            foreach (self::$_time_formats as $format) {

                if ($difference < $format[0]) {

                    if (count($format) == 2) {
                        $message = $format[1] . ($format[0] === 60 ? '' : ' ago');
                        break;
                    } else {
                        $message = ceil($difference / $format[2]) . ' ' . $format[1] . ' ago';
                        break;
                    }

                }
            }

        }

        return sprintf('<abbr title="%sZ">%s</abbr>',
            $date_from->get('YYYY-MM-ddTHH:mm:ss'),
            $message
        );
    }
}
leek