PHP如何查找从日期开始的时间? [英] PHP How to find the time elapsed since a date time?
本文介绍了PHP如何查找从日期开始的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何找到自 2010-04-28 17:25:43
之后的日期时间戳所花费的时间,最终输出的文字应该像 xx Minutes Ago
/ xx Days Ago
How to find the time elapsed since a date time stamp like 2010-04-28 17:25:43
, final out put text should be like xx Minutes Ago
/xx Days Ago
推荐答案
大多数答案都围绕将日期从字符串转换为时间。你似乎主要考虑将日期设置为5天前的格式等。对吗?
Most of the answers seem focused around converting the date from a string to time. It seems you're mostly thinking about getting the date into the '5 days ago' format, etc.. right?
这是我如何去做:
$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':'');
}
}
我没有测试过,但它应该工作。
I haven't tested that, but it should work.
结果如下
event happened 4 days ago
或
event happened 1 minute ago
欢呼
这篇关于PHP如何查找从日期开始的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文