Date formatting with PHP from weird Twitter format

Through the twitter API, I was able to get the date and time when something was on Twitter

Jul 25 17:42:55 +0000 2013 

Now, in PHP, how do I get this in standard unix:

 2013-6-25 17:42:55 

I'm not sure what to do with datetime, but I think there is an easier way to do this, instead of parsing and modifying things with str_replace and substr

+4
source share
2 answers

Use the DateTime class, in particular the static createFromFormat() method

 $dt = DateTime::createFromFormat('M j H:i:s P Y', 'Jul 25 17:42:55 +0000 2013'); echo $dt->format('Ymd H:i:s'); 

A working example is http://codepad.viper-7.com/gLdEll

+7
source

Pass it through strtotime . Please note that this includes the time zone +0000, so the time will also be translated relative to your time zone.

 <?php $date = "Jul 25 17:42:55 +0000 2013"; echo date("Ymd H:i:s", strtotime($date)); 
+5
source

Source: https://habr.com/ru/post/1493569/


All Articles