我在下面有一个简单的函数,它可以获取秒数并像“2 分钟前”、“9 小时前”等返回它......
如果我在 'relativedate()' 函数上传递 1,它只会返回 1 秒,依此类推。众所周知,60*60*24*7*30 或 18144000sec = 1 个月。因此,如果我在参数上传递 18144000 的值,它应该返回 1 个月。但是,如果我在参数上传递一个值,让我们在 relativedate() 上说 231440000 应该超过一年,它会返回“13 个月”而不是 1 年。
function relativedate($secs) {
$second = 1;
$minute = 60;
$hour = 60*60;
$day = 60*60*24;
$week = 60*60*24*7;
$month = 60*60*24*7*30;
$year = 60*60*24*7*30*365;
if ($secs <= 0) { $output = "now";
}elseif ($secs > $second && $secs < $minute) { $output = round($secs/$second)." second";
}elseif ($secs >= $minute && $secs < $hour) { $output = round($secs/$minute)." minute";
}elseif ($secs >= $hour && $secs < $day) { $output = round($secs/$hour)." hour";
}elseif ($secs >= $day && $secs < $week) { $output = round($secs/$day)." day";
}elseif ($secs >= $week && $secs < $month) { $output = round($secs/$week)." week";
}elseif ($secs >= $month && $secs < $year) { $output = round($secs/$month)." month";
}elseif ($secs >= $year && $secs < $year*10) { $output = round($secs/$year)." year";
}else{ $output = " more than a decade ago"; }
if ($output <> "now"){
$output = (substr($output,0,2)<>"1 ") ? $output."s" : $output;
}
return $output;
}
echo relativedate(60); // 1 minute