-1

I've search a lot of times and only get converting date from Y-m-d to d-m-Y like this.

$date = new DateTime('2000-01-01');
echo $date->format('d-m-Y H:i:s');

But I have a string in d-m-Y and want to converte to Y-m-d.

It's just the reverse but I don't know how to do.

===

My own answer

People said that my question is already questioned. But people don't understand that my date var is a STRING and return erro when I tried what they say as a solution. My date var is a STRING in this format DD/MM/YYYY, I discover the solution searching it in pt-br on google. This is the solution.

function data_user_para_mysql($y){
    $data_inverter = explode("/",$y);
    $x = $data_inverter[2].'-'. $data_inverter[1].'-'. $data_inverter[0];
    return $x;
}
Community
  • 1
  • 1
Fabiano Luiz
  • 63
  • 1
  • 1
  • 8

2 Answers2

4

Simple:

$date->format('Y-m-d H:i:s');

The documentation should help you.

SeanWM
  • 16,789
  • 7
  • 51
  • 83
0

Other option without DateTime, would be:

echodate('Y-m-d',strtotime('01-01-2000'));

tomsseisums
  • 13,168
  • 19
  • 83
  • 145