views:

323

answers:

4

Hi i'm getting dates from feed in this format

2009-11-04T19:55:41Z

i'm trying to format it using the date() function in PHP but i get an error saying:

date() expects parameter 2 to be long, object given in /bla/bla.php

i tried using preg_replace() to remove the T and the Z but still can't get it to work

any ideas on this ?

A: 

You can use the strtotime function.

echo date('Y-M-D', strtotime($feedDate));
Brian Fisher
+5  A: 

strtotime is a wonderful function for converting date formats to Unix timestamps.

This will give you what you're after:

date('my format here', strtotime('2009-11-04T19:55:41Z'));
Josh Leitzel
Remember that `strtotime` uses the timezone it can find (for example in the `TZ` environment variable. The function documentation fails to give any pointers on what happens to timestamps that embed the time zone information.
Joey
worked great :) thanks
Yaniv
+1  A: 

That is the standard ISO 8601 combined date and time format given in UTC (hence the Z).

You might be able to parse it using

DateTime::createFromFormat('c', '2009-02-03');

or, if that fails (shouldn't, if PHP claims to understand ISO 8601), you can replace the Z by "+00:00".

Joey
A: 

Try using strptime:

$date = strptime($str, "Y-m-d\TH:i:s\Z");
Dominic Rodger