开发者

Convert Standard Date to Current Time in Hours/Mins/

开发者 https://www.devze.com 2023-04-11 04:57 出处:网络
Say I have a dat开发者_开发知识库e function that produces the output: 2011-10-03 PHP: $todayDt = date(\'Y-m-d\');

Say I have a dat开发者_开发知识库e function that produces

the output: 2011-10-03

PHP:

$todayDt = date('Y-m-d');

Anyway to get this date to instead show 2 days 1 hour ago


This function might be of some use. You might want to refine the check for months a bit, but this is just a quick example:

function RelativeTime($iTimestamp, $iLevel = 2)
{
    !ctype_digit($iTimestamp)
        && $iTimestamp = strtotime($iTimestamp);

    $iSecondsInADay = 86400;
    $aDisplay = array();

    // Start at the largest denominator
    $iDiff = time() - $iTimestamp;
    $aPeriods = array(
        array('Period'  => $iSecondsInADay * 356,   'Label'   => 'year'),
        array('Period'  => $iSecondsInADay * 31,    'Label'   => 'month'),
        array('Period'  => $iSecondsInADay,         'Label'   => 'day'),
        array('Period'  => 3600,                    'Label'   => 'hour'),
        array('Period'  => 60,                      'Label'   => 'minute'),
        array('Period'  => 1,                       'Label'   => 'second'),
    );

    foreach ($aPeriods as $aPeriod)
    {
        $iCount = floor($iDiff / $aPeriod['Period']);
        if ($iCount > 0)
        {
            $aDisplay[] = $iCount . ' ' . $aPeriod['Label'] . ($iCount > 1 ? 's' : '');
            $iDiff -= $iCount * $aPeriod['Period'];
        }
    }

    $iRange = count($aDisplay) > $iLevel
                ? $iLevel
                : count($aDisplay);
    return implode(' ', array_slice($aDisplay, 0, $iRange)) . ' ago';
}

And some examples of usage:

echo RelativeTime(time() - 102, 1);
// Will output: 1 minute ago

echo RelativeTime(time() - 2002);
// Will output: 33 minutes 22 seconds ago

echo RelativeTime(time() - 100002002, 6);
// Will output: 3 years 2 months 27 days 10 hours 20 minutes 2 seconds ago

echo RelativeTime('2011-09-05');
// Will output: 30 days 22 hours ago


This is post is just for a solution that does not use the DateTime::diff method. It also uses inputs with greater precision, so be aware of that.

$now = date('Y-m-d H:i:s');
$then = '2011-10-03 00:00:00';  // This will calculate the difference 
                                // between now and midnight October 3rd
$nowTime = strtotime($now);
$thenTime = strtotime($then);

$diff = $nowTime - $thenTime;

$secs = $diff % 60;
$diff = intval($diff / 60);
$minutes = $diff % 60;
$diff = intval($diff / 60);
$hours = $diff % 24;
$diff = intval($diff / 24);
$days = $diff;


echo($days . ' days ' . $hours . ' hours ' . $minutes . ' minutes ' . $secs . ' seconds ago');

At the moment I tested it, the output was:

2 days 16 hours 6 minutes 2 seconds ago

If all you want are the days and hours, then you can just choose to echo those two out:

echo($days . ' days ' . $hours . ' hours ago');

2 days 16 hours ago

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号