如何获得过去的日期时间或分钟

时间:2015-12-09 14:52:03

标签: php

如何从此数据时间字段中获取分钟数

$time = strtotime('2010-04-28 17:25:43');

echo 'event happened '.humanTiming($time).' ago';

function humanTiming ($time)
{

$time = time() - $time; // to get the time since that moment
$time = ($time<1)? 1 : $time;
$tokens = array (
    31536000 => 'year',
    2592000 => 'month',
    604800 => 'week',
    86400 => 'day',
    3600 => 'hour',
    60 => 'minute',
    1 => 'second'
);

foreach ($tokens as $unit => $text) {
    if ($time < $unit) continue;
    $numberOfUnits = floor($time / $unit);
    return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}

}

2 个答案:

答案 0 :(得分:0)

使用DateTime,您可以像这样实现:

$d1 = new DateTime('2010-04-28 17:25:43'); // start time
$d2 = new DateTime(); // end time (now)

$diff_seconds = $d2->getTimestamp() - $d1->getTimestamp();
$diff_minutes = $diff_seconds/60; // get amount of minutes
echo round($diff_minutes);

答案 1 :(得分:-1)

将两者转换为时间戳,再次减去和格式化,例如

$timestamp = strtotime('22-09-2008');
$since = time() - $timestamp;
echo date('Y-m-d H:i:s', strtotime($since));

未经测试,请查看日期功能文档和此答案: How to calculate the difference between two dates using PHP?