php strtotime反向
发布时间:2020-05-27 07:45:13 所属栏目:PHP 来源:互联网
导读:在 PHP中是否有一些函数timetostr将在今天/明天/下一个星期日/等输出.从给定的时间戳?所以timetostr(strtotime(x))= x 这可能对来这里的人有用. /** * Format a timestamp to display its age (5 days ago, in 3 days, etc.). * * @param int $timestamp
|
在 PHP中是否有一些函数timetostr将在今天/明天/下一个星期日/等输出.从给定的时间戳?所以timetostr(strtotime(x))= x 这可能对来这里的人有用. /**
* Format a timestamp to display its age (5 days ago,in 3 days,etc.).
*
* @param int $timestamp
* @param int $now
* @return string
*/
function timetostr($timestamp,$now = null) {
$age = ($now ?: time()) - $timestamp;
$future = ($age < 0);
$age = abs($age);
$age = (int)($age / 60); // minutes ago
if ($age == 0) return $future ? "momentarily" : "just now";
$scales = [
["minute","minutes",60],["hour","hours",24],["day","days",7],["week","weeks",4.348214286],// average with leap year every 4 years
["month","months",12],["year","years",10],["decade","decades",["century","centuries",1000],["millenium","millenia",PHP_INT_MAX]
];
foreach ($scales as list($singular,$plural,$factor)) {
if ($age == 0)
return $future
? "in less than 1 $singular"
: "less than 1 $singular ago";
if ($age == 1)
return $future
? "in 1 $singular"
: "1 $singular ago";
if ($age < $factor)
return $future
? "in $age $plural"
: "$age $plural ago";
$age = (int)($age / $factor);
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
