2014-03-25 77 views
2

我正在使用插件来创建twitter feed中的wordpress帖子,并且我正在尝试对其进行编辑,以便发布的时间与推文时间相同,而不是cron的运行时间。发布日期date_create_from_format()

不幸的是,twitter的API返回一个已经格式化的日期字符串,而不是时间戳,所以我不得不解析它,然后以wordpress友好的格式保存它。

// Wed Jun 06 20:07:10 +0000 2012 (Twitter formatted date example) 
// 2014-03-10 18:30:26    (Wordpress formatted date example) 
$tweet_date = $tweet->created_at; 
$tweet_date = date_create_from_format("D M d h:i:s O Y", $tweet_date); 
$tweet_date = date("Y-m-d h:i:s", $tweet_date); 

不幸的是,我从这个Unix Epoch(1970年1月1日)得到的结果。

我知道我必须错过一个步骤,但我不知道在哪里。

回答

2

你有两个问题:

1)当你的意思是H 24小时的时间
2)你需要使用date_create_from_format()时,作为函数返回一个DateTime对象,它是使用date_format()您使用h数小时不兼容date()

$tweet_date = date_create_from_format("D M d H:i:s O Y", 'Wed Jun 06 20:07:10 +0000 2012'); 
echo date_format($tweet_date, 'Y-m-d H:i:s'); 

See it in action

+1

我没有注意到这一点。 +1超快速和演示。 –

+0

完美。由于早期的回应,我已经接近工作了,但仍然挂在我不正确的'h'上。谢谢! – Brownski

1

公关因为你在PHP的旧式和新式日期处理之间进行混合和匹配。

date_create_from_format()是新API的一部分,并输出DateTime对象,而不是旧版date()函数期望的时间戳整数。

理想情况下,您应该完全使用新的或旧的日期功能。你可以在它们之间切换,但通常不需要。

例如,在你的情况下,通过date_create_from_format()产生的DateTime对象具有完全可用format()方法连接到它,这确实完全一样的date()功能,但DateTime对象上。

$tweet_date_object = date_create_from_format("D M d h:i:s O Y", $tweet_date); 
$tweet_date = $tweet_date_object->format("Y-m-d h:i:s"); 
+0

请注意:'date_format()'是做' - > format()'的程序方式。 –