0

Need help in date conversion:

Existing date format:

Wed, 01 Dec 2010 00:00:00 -0500

(Day, DD MMM YYYY HH:MM:SS GMT-0500)

To be changed to:

2010-11-29T04:59:59-05:00

(YYYY-MM-DD(T)HH:MM:SS GMT-05:00)

How to handle in PHP?

is there any function available in php for this.

please help

Anthony
  • 36,459
  • 25
  • 97
  • 163
gourav
  • 1,397
  • 5
  • 20
  • 31
  • Why don't just store timestamp and convert it to date format later? – Shoe Nov 26 '10 at 18:51
  • 2
    possible duplicate of [PHP convert one date into another date format](http://stackoverflow.com/questions/2167916/php-convert-one-date-into-another-date-format) – Pekka Nov 26 '10 at 18:51

3 Answers3

3

strtotime() (man page) & date() (man page) or DateTime class (man page) should be able to handle this.

echo date('Y-m-d\TH:i:sZ', strtotime('Wed, 01 Dec 2010 00:00:00 -0500'));
echo date('c', strtotime('Wed, 01 Dec 2010 00:00:00 -0500')); // as mentioned by Anthony

or

echo DateTime('Wed, 01 Dec 2010 00:00:00 -0500')->format('Y-m-d\TH:i:sZ');
echo DateTime('Wed, 01 Dec 2010 00:00:00 -0500')->format('c'); // as mentioned by Anthony
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
2

First you want the date string in epoch format, so that you can use the date function. My favorite method to do this is the strtotime function:

$epoch_date = strtotime($original_date_string);

Now, you can use the date function to output it however you like. In your case, I believe you are looking for ISO 8601, which is built into the function:

 $new_date_string = date('c', $epoch_date);
 echo $new_date_string;
Anthony
  • 36,459
  • 25
  • 97
  • 163
  • Thanks for your reply. I am not looking for ISO 8601. can you please explain more on this. – gourav Nov 26 '10 at 19:00
  • The date manual page has a list of all possible formatting for output. Treffynnon's answer has the full formatted output: `Y-m-d\TH:i:sZ` – Anthony Nov 26 '10 at 19:03
  • You may not be looking for ISO 8601 formatted dates, but the format you specified is one of the formats specified by the ISO 8601 standard and routines that advertise ISO 8601 outputs may give you exactly what you are looking to achieve. – J Edward Ellis Nov 26 '10 at 19:28
1

date('Y\-m\-d\Th:i:s \G\M\TP');

This will return:
2010-11-26T02:49:24 GMT-05:00

Use the date() formating its much simpler!

You can read all about it right here http://php.net/manual/en/function.date.php

austinbv
  • 9,297
  • 6
  • 50
  • 82