【问题标题】:unable to get correct using strtotime("2015-02-14") function in php在 php 中使用 strtotime("2015-02-14") 函数无法正确
【发布时间】:2015-02-14 15:08:22
【问题描述】:

我想要在 strtotime() 函数中使用的日期时间戳。但它给了我该日期前一天的时间戳。 假设我使用:

echo strtotime("2015-02-14");

然后它给我1423868400 this,这是“2015-02-13”的时间戳。

请帮帮我。

【问题讨论】:

    标签: php date datetime strtotime


    【解决方案1】:

    可能是timezone 的问题。您可以将timezone 设置如下:

    date_default_timezone_set('UTC');
    echo strtotime("2015-02-14"); // Shows 1423872000
    

    编辑:

    // With your timezone
    date_default_timezone_set('Asia/Kolkata');
    echo strtotime("2015-02-14"); // Shows 1423852200
    


    您可以在此处阅读有关date_default_timezone_set 方法的更多信息:http://php.net/manual/en/function.date-default-timezone-set.php

    还有更多关于strtotime方法的信息也在这里:http://php.net/manual/en/function.strtotime.php

    【讨论】:

    【解决方案2】:

    您可能在这里遇到了时区问题。通过给出一个没有任何时间的日期,它假定您说的是 0h0min0sec...无论服务器是在格林威治之前还是之后,都可能会改变最终结果;)

    在发布你的脚本之前尝试setTimeZone到你当前的工作时区,这可能是解决方案。

    【讨论】:

    • 我已经在下面的代码中使用了 settimezone。 date_default_timezone_set('亚洲/加尔各答');
    • 正如 tomloprod 在他最新的编辑中所写:date_default_timezone_set('Asia/Kolkata'); echo strtotime("2015-02-14"); // Shows 1423852200 // then echo date("Y-m-d",1423852200); // Shows 2015-02-14 到目前为止应该没有问题......
    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多