gourav
gourav

Reputation: 1397

PHP date format conversion

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

Upvotes: 0

Views: 4705

Answers (3)

austinbv
austinbv

Reputation: 9491

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

Upvotes: 1

Treffynnon
Treffynnon

Reputation: 21553

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

Upvotes: 3

Anthony
Anthony

Reputation: 37045

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;

Upvotes: 2

Related Questions