我找到了一个SC函数来输出人类可读时间。喜欢`
5小时,1小时,5年等
function human_time ($time)
{
$time = time() - strtotime($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':'');
}
}
我有时间作为字符串:2013-09-28 20:55:42
当我调用此函数human_time('2013-09-28 20:55:42')
strtotime
。
请告诉我有什么问题。
答案 0 :(得分:1)
这不是现成的代码,而是应该以正确的方式为您提供指导:
$then = new DateTime($time); // you might need to format $time using strtotime or other functions depending on the format provided
$now = new DateTime();
$diff = $then->diff($now, true);
echo $diff->format('Your style goes here');
有关详细文档,请参阅DateTime Manual或随时在此处提问。
编辑:链接已修复。
答案 1 :(得分:1)
echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('2013-05-01 00:22:35', true);
4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
<强> Link to the function. 强>