Return correct date strtotime php

1

I have the following line

echo date('Y-m-d', strtotime("28/09/2016")); // 1970-01-01

How do I get back 2016-09-28? If the day is less than 12, it returns true ...

    
asked by anonymous 17.08.2016 / 15:23

2 answers

2

You need to pass as a parameter to strtotime a string with separator using - (hyphen) for example;

$date = str_replace("/", "-", "28/09/2016");
echo date("Y-m-d", strtotime($date));
    
17.08.2016 / 15:43
1

You can do this using the DateTime object of PHP:

$formato = 'd/m/Y'; // define o formato de entrada para dd/mm/yyyy
$data = DateTime::createFromFormat($formato, "28/09/2016"); // define data desejada
echo $data->format('Y-m-d'); // formata a saída

Read more about the supported formats in PHP - DATE

    
17.08.2016 / 15:46