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

2
  • Why don't just store timestamp and convert it to date format later? Commented Nov 26, 2010 at 18:51
  • 2
    possible duplicate of PHP convert one date into another date format Commented Nov 26, 2010 at 18:51

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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;

3 Comments

Thanks for your reply. I am not looking for ISO 8601. can you please explain more on this.
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
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.