1

How do I convert the date format from 2012-03-19T22:10:56.000Z to Wed Nov 13 18:06:37 +0000 2013 without using string functions like substr. This date format is being used by Gnip. Not sure what format it is.

I tried using the date function

$postedTime = "2012-03-19T22:10:56.000Z";
date($postedTime,"D M d H:i:s O Y"); 

But I get an error since $postedTime is a string and not long variable.

lonesomeday
  • 224,675
  • 49
  • 309
  • 312
db1
  • 2,639
  • 3
  • 14
  • 13

3 Answers3

3

Why use substr() when you can use functions designed specifically for working with dates?

$date = new DateTime('2012-03-19T22:10:56.000Z');
echo $date->format('r Y');

See it in action

John Conde
  • 212,985
  • 98
  • 444
  • 485
  • Thank you that worked. Although i changed the format $date = new DateTime('2012-03-19T22:10:56.000Z'); echo $date->format("D M d H:i:s O Y"); – db1 Dec 12 '13 at 21:10
1

You need to swap the order in your date function

date('format string', $time)

So you want:

date("D M d H:i:s O Y", strtotime($postedTime)); 

http://www.php.net/manual/en/function.date.php

user602525
  • 3,068
  • 4
  • 23
  • 39
0

You can transform the string in a DateTime object with

$date = new DateTime($postedTime);

Then you may need to change the time zone (since your postedTime is timezone Z for Zulu)

$date->setTimezone(new DateTimeZone('Europe/Rome'));

and maybe also add or subtract a suitable offset DateInterval.

Finally, if you need a timestamp for use in date, you do so with [getTimeStamp][1].

$ts  = $date->getTimeStamp();

If you have all datetimes in Zulu, just use

$date = new DateTime($postedTime);
date("Y-m-d H:i:s", $date->getTimeStamp());

or if you do not need the timestamp,

$date = new DateTime($postedTime);
print $date->format('Y-m-d H:i:s');

You should get the same date you entered. If it is offset one or more hours plus or minus, you have a time zone problem.

LSerni
  • 53,899
  • 9
  • 63
  • 106