1
A
回答
1
不原生地,但我的回答a similar question可能对你有用。
6
刚刚发现一个函数,它不正是我需要的
1
有没有内置该功能....但下列函数将做到这一点。
<?php
function nicetime($date)
{
if(empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
$date = "2009-03-04 17:45";
$result = nicetime($date); // 2 days ago
?>
-1
$date = "2009-03-04 17:45";
$result = nicetime($date);
只会返回2 Days Ago
但如果u想要得到像1秒前的结果,10秒前,U还需要在$日期添加秒
$date = "2009-03-04 17:45:20";
$result = nicetime($date);
所以,当你添加一个新的数据库条目,并立即刷新页面,你会得到结果为1秒前
相关问题
- 1. 将时间戳转换为X天X小时X分钟前
- 2. 转换x小时x分钟前回到unix时间戳
- 3. X分钟和Y秒前
- 4. PHP:一个小时前,X小时前
- 5. 的Javascript转换日期时间为 “______秒/分钟/小时/月前”
- 6. BUKKIT:如何等待X行时间X前
- 7. 最快显示x天,x小时,x分钟,x秒
- 8. 格式mysql TIMESTAMP AS x天,x小时,x分钟,x秒
- 9. How do you say在“X分钟前”或“X小时前”或“X天前”在Ruby中发生了什么?
- 10. 时间戳x小时前和24小时后的日期
- 11. python:如何将“x天前”或“x月前”转换为日期?
- 12. 显示MysQl行小于或等于15分钟前时间戳
- 13. 如果时间少于24小时前显示x时间前
- 14. 在关闭程序之前等待x秒钟C++
- 15. 安卓:时间毫秒到X日 - X小时 - X分 - X秒内离开
- 16. XSL将时间值转换为“分钟/小时/天前”格式
- 17. 时间戳(一分钟前或一小时前)
- 18. 时间戳为天,小时,分钟,秒
- 19. 转换标准日期为当前时间以小时/分钟]/
- 20. 在将值添加到可观察值前等待x秒
- 21. 组时间X分钟,分裂在X分钟间隔
- 22. 将时间戳转换为天数小时前
- 23. 将小时,分钟和秒添加到当前时间
- 24. MSBuild - 等待x秒
- 25. Twitter的风格X分钟前
- 26. 将秒转换为小时和分钟
- 27. UNIX时间戳小时前
- 28. 转换“时间前”秒为Android
- 29. Unix时间戳到秒,分钟,小时
- 30. 如果当前页面的URL等于X或X?
可能重复的[Converti ng timestamp to time before in php例如1天前,2天前...](http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-eg-1-日前2天前)为[更多](http://stackoverflow.com/search?q=php+date+days+ago) – ajreal 2010-12-21 06:10:50