Return correct php strtotime date

Asked

Viewed 7,685 times

3

I have the following line

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

How do I return 2016-09-28? If the day is less than 12, returns...

  • 1

    the string inside the strtotime has to have the separator with - also, try this.

  • The funny thing is that if I put a day shorter than 12 months, returns normal

  • That was it Rafael, echo date('Y-m-d', strtotime(str_replace("/", "-", "28/09/2016")); thanks buddy. Call me in skype K7, you add!

  • Put as an answer, to give the acceptance here.

2 answers

4

Can do using the object DateTime 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

3


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

$date = str_replace("/", "-", "28/09/2016");
echo date("Y-m-d", strtotime($date));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.