-2

I have a date:

Mon, 31 Mar 2014 12:19:10 GMT

How can I convert that to this format:

date('Y-m-d H:i:s')

I have found something like this:

$time = strtotime('10/16/2003');

$newformat = date('Y-m-d',$time);

But how do I convert 'Mar' to '03', so I can use that?

gray
  • 798
  • 18
  • 31
  • 3
    Read [`DateTime::createFromFormat`](http://www.php.net/manual/en/datetime.createfromformat.php) – Shankar Narayana Damodaran Apr 06 '14 at 14:30
  • If your date format is *not* consistent: Use [`DateTime::createFromFormat()`](http://www.php.net/manual/en/datetime.createfromformat.php) to parse the date string and create a DateTime object. And then format it with [`DateTime::format()`](http://www.php.net/manual/en/datetime.format.php). The list of formats can be found in the [documentation here](http://php.net/date). – Amal Murali Apr 06 '14 at 14:32
  • strtotime() is very flexible; have you tried just giving it your current string? – IMSoP Apr 06 '14 at 14:32

3 Answers3

1

No need, PHP's strtotime understands month names.

C:\Users\Niet>php
<?php
var_dump(date('Y-m-d H:i:s',strtotime('Mon, 31 Mar 2014 12:19:10 GMT')));
^Z

string(19) "2014-03-31 12:19:10"
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

How about using DateTime

$date = new DateTime("Mon, 31 Mar 2014 12:19:10 GMT");

echo $date->format("Y-m-d H:i:s");
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
1

strtotime does the trick here too.

This code:

$date = 'Mon, 31 Mar 2014 12:19:10 GMT';
echo $date;
$time = strtotime('Mon, 31 Mar 2014 12:19:10 GMT');
echo date('Y-m-d H:i:s', $time);

will output this:

Mon, 31 Mar 2014 12:19:10 GMT
2014-03-31 14:19:10

Note that your timezone is important here too. I'm in GMT+2 timezone, so final hour is 14 instead of 12.

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64