Create datetime form dd/mm/yy(yy)

206 Views Asked by At

I try to create datetime form dd/mm/yy(yy), but with no success?

code:

var_dump(date_create("22/12/2016"));

or

var_dump(strtotime("22/12/2016"));

neither works. This is the demo. Why I cannot create from this format?

1

There are 1 best solutions below

0
On BEST ANSWER

before date_create convert your date into an acceptable date format using

DateTime::createFromFormat (PHP 5 >= 5.3.0, PHP 7) DateTime::createFromFormat -- date_create_from_format — Parses a time string according to a specified format

var_dump(date_create(DateTime::createFromFormat('d/m/Y', "22/12/2016")->format('Y-m-d')));

Demo