Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting Date with PHP from weird twitter format

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

like image 748
Joel Werner Avatar asked Jul 26 '13 03:07

Joel Werner


People also ask

How do I change date format from YYYY MM DD in PHP?

Change YYYY-MM-DD to DD-MM-YYYY$newDate = date("d-m-Y", strtotime($orgDate));

What is twitter date format?

Time is represented using a subset of ISO-8601. More specifically, the strptime string for our date format is "%Y-%m-%dT%l:%M:%S%z".

What is default format of date in PHP?

Mysql default date format is Year-Month-Date that is why every PHP developer has with a problem. $date = "2019-02-08" ; $newDate1 = date ( "m-d-Y" , strtotime ( $date )); $newDate2 = date ( "l M, d, Y" , strtotime ( $date ));


1 Answers

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

like image 57
Phil Avatar answered Sep 23 '22 12:09

Phil