php strtotime反向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8629788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:08:00  来源:igfitidea点击:

php strtotime reverse

phpstrtotime

提问by prongs

Is there some function timetostrin php that will output today/tomorrow/next sunday/etc.from a given timestamp? So that timetostr(strtotime(x))=x

timetostrphp 中是否有一些函数可以today/tomorrow/next sunday/etc.从给定的时间戳输出?以便timetostr(strtotime(x))=x

回答by rich remer

This might be useful for people coming here.

这可能对来这里的人有用。

/**
 * 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);
    }
}

回答by barakadam

There cannot be a strtotimereverse function because this is not a bijection. The source string from which you get a UNIX timestamp when you use strtotimecan be formatted in many different ways. So if you decide to reverse the function, how can you know what string format to use ? It could well be 2010-08-05 or 10 September 2000, etc. This is exactly why there is no reverse function, but as Andypandy rightly said, you have to use date()which allows you to actually define the string format you wish to end up with. I know this question is old, but I thought it deserved this answer so other users understand why there is no such function in PHP.

不可能有strtotime反向函数,因为这不是双射。使用时从中获取 UNIX 时间戳的源字符串strtotime可以采用多种不同方式进行格式化。因此,如果您决定反转该函数,您怎么知道要使用哪种字符串格式?它很可能是 2010-08-05 或 2000 年 9 月 10 日等。这正是为什么没有反向功能的原因,但正如 Andypandy 说得对,您必须使用date()它允许您实际定义您希望最终的字符串格式和。我知道这个问题很老,但我认为它值得这个答案,所以其他用户理解为什么 PHP 中没有这样的功能。