gpt4 book ai didi

php strtotime 反转

转载 作者:可可西里 更新时间:2023-11-01 13:13:45 24 4
gpt4 key购买 nike

php 中是否有一些函数 timetostr 会根据给定的时间戳输出 today/tomorrow/next sunday/etc.?所以 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", 10],
["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);
}
}

关于php strtotime 反转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8629788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com