Return to Snippet

Revision: 12587
at March 19, 2009 19:58 by oscarduignan


Updated Code
/**
 * @var     timestamp
 * @var     timestamp
 * @return  string approximate time, for example: about 19 hours
 */
function getTimeDifferenceInWords($firstTime, $secondTime = '')
{
    // convert to unix timestamps
    $firstTime = strtotime($firstTime);
    // if second time was not supplied, use current time
    $secondTime = ($secondTime) ? strtotime($secondTime) : time();
        
    // find out the difference in seconds
    $seconds = ($firstTime > $secondTime) 
        ? $firstTime - $secondTime 
        : $secondTime - $firstTime;

    $minutes = floor($seconds / 60);
    if ($minutes == 0) { return 'less than a minute'; }
    if ($minutes == 1) { return 'a minute'; }
    if ($minutes < 45) { return $minutes . ' minutes'; }

    $hours = round($minutes / 60);
    if ($hours <= 1) { return 'about 1 hour'; }
    if ($hours < 24) { return 'about ' . $hours . ' hours'; }

    $days = round($hours / 24);
    if ($days == 1) { return 'about 1 day'; }
    if ($days < 30) { return 'about ' . $days . ' days'; }
        
    $months = round($days / 30);
    if ($months == 1) { return 'about 1 month'; }
    if ($months < 12) { return 'about ' . $months . ' months'; }

    $years = round($months / 12);
    if ($years == 1) { return 'about 1 year'; }
    return 'about ' . $years . ' years';
}

Revision: 12586
at March 19, 2009 19:50 by oscarduignan


Initial Code
<?php
/**
* Recreates the behavior of the rails method distance_of_time_in_words
* @author   Oscar Duignan
*/
class Blog_View_Helper_GetTimeDifferenceInWords extends Zend_View_Helper_Abstract
{
    /**
     * @var     timestamp
     * @var     timestamp
     * @return  string approximate time, for example: about 19 hours
     */
    function getTimeDifferenceInWords($firstTime, $secondTime = '')
    {
        // convert to unix timestamps
        $firstTime = strtotime($firstTime);
        // if second time was not supplied, use current time
        $secondTime = ($secondTime) ? strtotime($secondTime) : time();
        
        // find out the difference in seconds
        $seconds = ($firstTime > $secondTime) 
            ? $firstTime - $secondTime 
            : $secondTime - $firstTime;

        $minutes = floor($seconds / 60);
        if ($minutes == 0) { return 'less than a minute'; }
        if ($minutes == 1) { return 'a minute'; }
        if ($minutes < 45) { return $minutes . ' minutes'; }

        $hours = round($minutes / 60);
        if ($hours <= 1) { return 'about 1 hour'; }
        if ($hours < 24) { return 'about ' . $hours . ' hours'; }

        $days = round($hours / 24);
        if ($days == 1) { return 'about 1 day'; }
        if ($days < 30) { return 'about ' . $days . ' days'; }
        
        $months = round($days / 30);
        if ($months == 1) { return 'about 1 month'; }
        if ($months < 12) { return 'about ' . $months . ' months'; }

        $years = round($months / 12);
        if ($years == 1) { return 'about 1 year'; }
        return 'about ' . $years . ' years';
    }
}

Initial URL


Initial Description
Simple function to recreate the distance_of_time_in_words function from rails.

Initial Title
GetTimeDifferenceInWords

Initial Tags


Initial Language
PHP