Do you know how I can convert this to a strtotime, or a similar type of value to pass into the DateTime object?
The date I have:
Mon, 12 Dec 2011 21:17:52 +0000
What I’ve tried:
$time = substr($item->pubDate, -14);
$date = substr($item->pubDate, 0, strlen($time));
$dtm = new DateTime(strtotime($time));
$dtm->setTimezone(new DateTimeZone(ADMIN_TIMEZONE));
$date = $dtm->format('D, M dS');
$time = $dtm->format('g:i a');
The above is not correct. If I loop through a lot of different dates its all the same date.
You don’t need to turn the string into a timestamp in order to create the
DateTimeobject (in fact, its constructor doesn’t even allow you to do this, as you can tell). You can simply feed your date string into theDateTimeconstructor as-is:That being said, if you do have a timestamp that you wish to use instead of a string, you can do so using
DateTime::setTimestamp():Edit (2014-05-07):
I actually wasn’t aware of this at the time, but the
DateTimeconstructor does support creating instances directly from timestamps. According to this documentation, all you need to do is prepend the timestamp with an@character: