Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Date String Format [duplicate]

Tags:

date

php

cakephp

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 ?

like image 382
Harsha M V Avatar asked Apr 28 '12 21:04

Harsha M V


2 Answers

$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');
like image 102
John Conde Avatar answered Oct 05 '22 06:10

John Conde


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
like image 44
Baba Avatar answered Oct 05 '22 06:10

Baba