Reputation: 23
I am using a plugin to create wordpress posts from a twitter feed, and I am trying to edit it so the published time is the same as the tweeted time, rather than the time the cron was run.
Unfortunately, twitter's API returns an already formatted date string, instead of a timestamp, so I am having to parse it and then save it in a wordpress friendly format.
// 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);
Unfortunately, all I am getting from this the Unix Epoch (Jan 1st, 1970).
I know I must be missing a step, but I can't figure out where.
Upvotes: 2
Views: 3679
Reputation: 168755
The problem is because you're mixing and matching between PHP's old and new-style date handling.
date_create_from_format()
is part of the newer API, and outputs a DateTime
object, not the timestamp integer that the older date()
function is expecting.
Ideally you should stick entirely with either the new or the old date functions. You can switch between them, but there usually isn't a need to.
For example, in your case, the DateTime
object generated by date_create_from_format()
has a perfectly usable format()
method attached to it, which does exactly the same as the date()
function, but on a DateTime
object.
$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");
Upvotes: 1
Reputation: 219864
You had two issues:
1) You were using h
for hours when you meant H
for 24 hour periods
2) You need to use date_format()
when using date_create_from_format()
as that function returns a DateTime object which is not compatible with 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');
Upvotes: 3