5

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

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Joel Werner
  • 103
  • 1
  • 8

2 Answers2

9

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

Phil
  • 157,677
  • 23
  • 242
  • 245
  • 11
    Twitter feed stamps look like "Mon Sep 08 15:19:11 +0000 2014", correct format for these is $d = \DateTime::createFromFormat('D M d H:i:s P Y', (string)$x->created_at); – Saeven Sep 09 '14 at 17:04
8

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));
DevZer0
  • 13,433
  • 7
  • 27
  • 51