9

I have a string mm-dd-yyyy which i get from a form, which i want to store it in the database of the data-type DATE (yyyy-mm-dd).

How do i format the string and save it in the database ?

Harsha M V
  • 54,075
  • 125
  • 354
  • 529

2 Answers2

12
$new_format = date("Y-m-d", strtotime('04-28-2012'));

or

$date = new DateTime('04-28-2012');
$new_format = $date->format('Y-m-d');

or in PHP 5.5+

$new_format = (new DateTime('04-28-2012'))->format('Y-m-d');
John Conde
  • 217,595
  • 99
  • 455
  • 496
9

Try

$date = DateTime::createFromFormat("m-d-Y", '02-15-2012');
echo  $date->format('Y-m-d H:i:s') , "\n";

Output

2012-02-15 23:54:52
Baba
  • 94,024
  • 28
  • 166
  • 217