Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format date dd/mm/yyyy to yyyy-mm-dd PHP [duplicate]

Tags:

date

php

I have a form within which a date is input in UK format, and I need to convert it to

yyyy-mm-dd

e.g. a date entered is: 31/03/2013 which I want to convert to '2013-03-31' for database insert.

I'm using the following which bizarrely works only sometimes:

$dateInput = $mysqli->real_escape_string($_POST['date']);
$show_date = date('Y-m-d', strtotime($dateInput));

Is there a better way to do this?

like image 943
StudioTime Avatar asked Mar 28 '13 07:03

StudioTime


1 Answers

Try this :

$dte  = '28/03/2013';
$dt   = new DateTime();
$date = $dt->createFromFormat('d/m/Y', $dte);
echo $date->format('Y-m-d');

Output: 2013-03-28

like image 181
Prasanth Bendra Avatar answered Sep 24 '22 01:09

Prasanth Bendra