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
  • 52,333
  • 115
  • 334
  • 516

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
  • 212,985
  • 98
  • 444
  • 485
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
  • 92,047
  • 28
  • 163
  • 215