嗨,有人可以帮助我,我在 mysql 表中回显时间戳,但我希望 php 以天/小时/分钟计算时间戳。
这可能吗,我对 php 和 mysql 还是新手并且正在学习,所以有人可以指导我正确的方向或告诉我如何做到这一点。谢谢你
<? echo "{$news['date_added']}";?>
我希望输出为秒数:12 秒前添加或 3 分钟前添加或 4 天前添加或 4 个月前添加
我认为脚本应该如下所示是否正确:
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 = "{$news['content']}";
$result = nicetime($date); // 2 days ago ?>