【问题标题】:How to convert "2012-10-16T14:46:33+00:00" into Unix Timestamp in PHP?如何在 PHP 中将“2012-10-16T14:46:33+00:00”转换为 Unix 时间戳?
【发布时间】:2012-10-07 04:46:32
【问题描述】:

如何在 PHP 中将 2012-10-15T13:16:13+00:00 格式的字符串转换为 Unix 时间戳?

【问题讨论】:

    标签: php time format timestamp converter


    【解决方案1】:

    strtotime() 应该可以处理。

    例子

    echo strtotime("2012-10-15T13:16:13+00:00") ; // 1350306973
    

    或者

    [ghoti@pc ~]$ php -r '$d="2012-10-15T13:16:13+00:00"; print strtotime($d) . "\n";'
    1350306973
    

    一旦有了时间戳,就可以用它做任何事情。

    [ghoti@pc ~]$ php -r '$d="2012-10-15T13:16:13+00:00"; $t=strtotime($d); print strftime("%+", $t)."\n";'
    Mon Oct 15 09:16:13 EDT 2012
    

    【讨论】:

    • 当然,这完全没问题。我习惯于在命令行上显示东西,以帮助证明人们可以完全复制我在他们自己的系统上所做的事情(并希望与他们一起玩),但早期的简化也非常有帮助。 :)
    • @Baba:谢谢。新的 DateTime 类中是否有类似strtotime() 的功能?我找不到...
    【解决方案2】:

    Baba 提供的解决方案使用DateTime class

    $dt = new DateTime("2012-10-15T13:16:13+00:00");
    echo $dt->getTimestamp();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 2017-03-13
      • 2023-02-03
      相关资源
      最近更新 更多