Question

Through the twitter API I was able to get the datetime of when something was tweeted

Jul 25 17:42:55 +0000 2013

Now, in PHP, how do I get that into standard unix:

2013-6-25 17:42:55

I'm not to sure on anything to deal with datetime but I think there is an easier way to do this rather than having to parse through and change things with str_replace and substr

Was it helpful?

Solution

Use the DateTime class, specifically the static createFromFormat() method

$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');

Working example - http://codepad.viper-7.com/gLdEll

OTHER TIPS

Simply pass it through strtotime. Note that this includes a timezone +0000 so the time will translate relative to your timezone also.

<?php
$date = "Jul 25 17:42:55 +0000 2013";
echo date("Y-m-d H:i:s", strtotime($date));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top