-4

Possible Duplicate:
PHP date format converting

I have a date like 2012-september-09. How to convert this into 2012-09-09 using PHP?

Community
  • 1
  • 1
lalith222
  • 309
  • 5
  • 16
  • i try to convert 2012-september-09 to 2012-09-09 with date("Y-m-D",strtotime("2012-september-09")). but this returns 2012-09-01. so please tell me the correct method to convert this – lalith222 Sep 05 '12 at 06:57
  • there is dozens of similar questions asking how to format dates. please do research them before asking questions. – Gordon Sep 05 '12 at 06:59
  • 2
    `echo DateTime::createFromFormat('Y-M-d', '2012-september-09')->format('Y-m-d');` – Gordon Sep 05 '12 at 07:09

2 Answers2

2

You can try with strptime

$date = '2012-september-09';
$strp = strptime($date, '%Y-%B-%d');
$ymd = sprintf('%04d-%02d-%02d', $strp['tm_year'] + 1900, $strp['tm_mon'] + 1, $strp['tm_mday']);
$new_date = new DateTime($ymd);
echo $new_date->format('Y-m-d');

here's a Codepad

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
0

Try this

Change 

<?php echo date('Y-m-d',strtotime('2012-september-09'));?>

 To

<?php echo date('Y-m-d',strtotime('09-september-2012'));?>
Pramod Kumar Sharma
  • 7,851
  • 5
  • 28
  • 53