我有以下格式的时间戳:2013-10-19 18:47:30
我正在使用它来转换为相对(x 分钟、小时、天前)时间,但它不会为最近的用户返回任何内容,我假设因为我的系统时间是 GMT -3 小时,所以它将它解释为未来的时间. 如果是这种情况,我如何在结果中考虑用户 GMT 偏移量?
$time = strtotime('2013-04-28 17:25:43');
echo 'event happened '.humanTiming($time).' ago';
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$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':'');
}
}