Question

In my site, I have a bootstrap datepicker which allows user to pick date in format of MM/DD/YYYY (e.g: 05/12/2014). Then when this data is submitted, I used the following PHP code to convert it into Datetime type, then insert into start_date (DATETIME datatype) column in MySQL .

$start_date = date('Y-m-d', $_POST['start_date']);

the insert query in PHP does nothing with reformatting the date. It just simply insert into corresponding column.

However, instead of inserting '2014-05-12', the value inserted into database is '1970-01-01'. That's so weird to me. Can anybody tell me what's wrong here. Is this that I used incorrect PHP function or incorrect timezone setting or ...

Was it helpful?

Solution

Just do this:

$start_date = date('Y-m-d', strtotime($_POST['start_date']));

OTHER TIPS

You could also use strtotime() on your $_POST.

$start_date = date('Y-m-d', strtotime('05/12/2014'));

try to use

$date = str_replace('/', '-', $_POST['start_date']);
$start_date = date('Y-m-d', strtotime($date));

For more :- Converting between illogically formatted dates (changing /slash/ to -dash- )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top