【问题标题】:Timestamp to internal day count内部天数的时间戳
【发布时间】:2009-12-20 23:17:13
【问题描述】:

在一个项目中,我有一个内部计算时间。

仅列举了自发布以来的天数:

2009/10/19:发布日 2009 年 10 月 20 日:第 1 天 2009 年 10 月 21 日:第 2 天 等等

现在我想要一个函数,它可以给我当前日期的内部计算时间。例如。 2010 年的某一天“第 129 天”。

但保持这种准确性很重要:

2009/10/20 00:00:01 => 第 1 天 2009/10/20 23:59:59 => 第 1 天 2009/10/21 00:00:01 => 第 2 天

我构建了以下函数,但它没有那么准确。为什么不?你能改进它吗?

function date2daycount($timestamp) {
 $launchDay = 1255903200-3600*24; // launch (2009/10/19 00:00:00)
 $difference = floor(($timestamp-$launchDay)/86400); // days after launch
 return $difference;
}

提前致谢!

【问题讨论】:

    标签: php datetime function time timestamp


    【解决方案1】:
    function date2daycount($timestamp) {
     $launchDay =  strtotime('19 October 2009'); // launch (2009/10/19 00:00:00)
     $difference = floor(($timestamp-$launchDay)/86400); // days after launch
     return $difference+1;
    }
    

    【讨论】:

    • 感谢您的回答,但 strtotime('19 October 2009') 在我的服务器上返回 1255903200。这就是我在问题中写的内容。
    • 但是-3600*24 是干什么用的?
    • 回来升级系统修改这段代码时使用strtotime更加冗长易读
    • -3600*24 在那里,因为 10 月 19 日应该是第 1 天,而不是第 0 天。是的,strtotime 更详细。但这还不正确。 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    相关资源
    最近更新 更多