用PHP从奇怪的twitter格式格式化日期


Formatting Date with PHP from weird twitter format

通过twitter API,我能够获得推特上发布内容的日期时间

Jul 25 17:42:55 +0000 2013

现在,在PHP中,我如何将其引入标准的unix:

2013-6-25 17:42:55

我不确定是否有什么可以处理日期时间,但我认为有一种更简单的方法可以做到这一点,而不是必须使用str_replacesubstr 来解析和更改内容

使用DateTime类,特别是静态createFromFormat()方法

$dt = DateTime::createFromFormat('M j H:i:s P Y', 'Jul 25 17:42:55 +0000 2013');
echo $dt->format('Y-m-d H:i:s');

工作示例-http://codepad.viper-7.com/gLdEll

只需通过strtotime即可。请注意,这包括一个时区+0000,因此时间也将相对于您的时区进行转换。

<?php
$date = "Jul 25 17:42:55 +0000 2013";
echo date("Y-m-d H:i:s", strtotime($date));