我正在尝试创建用户之前的活跃时间。每1分钟,时间将在数据库中更新。其中“$ dbtime”是在数据库中更新的时间。这是我尝试的代码,但它只能正确计算30分钟。我想创造一年。保存在数据库中的时间和当前时间格式相同。请帮我解决这个问题。
$now = new datetime('now');
$current_time = $now->format('yjgis');
$my_tim = $current_time - $dbtime;
switch ($my_tim) {
case $my_tim < 60 && $my_tim > 0:
$a_tim = "Online";
break;
case $my_tim > 60 && $my_tim < 3600 :
$a_tim = round($my_tim/60) .' '."Min ago";
break;
case $my_tim > 3600 && $my_tim < 86400:
$a_tim = round(round($my_tim/60)/60).' '."Hour ago";
break;
default:
$a_tim = "Offline";
}
答案 0 :(得分:1)
您可以使用从CSS Tricks中提取的此功能:
<?php
function ago($time)
{
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$difference = $now - $time;
$tense = "ago";
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] 'ago' ";
}
echo ago($dbtime);
?>