I have the creation time in following format: Mon Mar 25 2013 15:28:21 GMT+0000 (GMT Standard Time) How can I use PHP to extract the date from this string?
Asked
Active
Viewed 441 times
-7
-
What do you want to do with the date? – iCode May 28 '14 at 13:45
-
5Have you seriously made a basic search before asking ? – BMN May 28 '14 at 13:45
-
You mean `Mar 25 2013`? – Albzi May 28 '14 at 13:46
-
I want the date in 2013-03-25 15:28:21 format – user1369905 May 28 '14 at 13:47
2 Answers
4
$dateTime = DateTime::createFromFormat('D M d Y H:i:s e', 'Mon Mar 25 2013 15:28:21 GMT+0000');
$dateTime->setTimezone(new DateTimeZone('UTC'));
echo $dateTime->format('Y-m-d H:i:s');
Note this requires at least PHP 5.3.0, for more information see documentation at http://www.php.net/manual/en/datetime.createfromformat.php

Barry Staes
- 3,890
- 4
- 24
- 30

Mark Baker
- 209,507
- 32
- 346
- 385
-
-
@barry-staes - If people are still developing for unsupported PHP versions < 5.3.0 then it is really time for them to upgrade now – Mark Baker May 28 '14 at 14:01
-
No need for `->setTimezone()` since DateTime object is already set to UTC offset. – Glavić May 28 '14 at 14:19
0
See http://php.net/manual/en/function.date-parse.php
Example #1 A date_parse() example
<?php print_r(date_parse("2006-12-12 10:00:00.5")); ?>
The above example will output:
Array ( [year] => 2006 [month] => 12 [day] => 12 [hour] => 10 [minute] => 0 [second] => 0 [fraction] => 0.5 [warning_count] => 0 [warnings] => Array() [error_count] => 0 [errors] => Array() [is_localtime] => )
From here on, you should be able to parse Mon Mar 25 2013 15:28:21 GMT+0000
. Or consider using explode(' ', $parts)
to split it into a $parts[2]='25'
array.

Barry Staes
- 3,890
- 4
- 24
- 30