-1

For example I have this string "2015.02.10 01:24:13". And I need it to be converted to timestamp format 2015-02-10 01:24:13. I tried this

$dtime = DateTime::createFromFormat("Y-m-d H:i:s",$new_data['query_start_date']); 
$post->query_start_date = $dtime->getTimestamp();

But it didnt work.

1 Answers1

2

It didn't work

Its because you were creating a wrong format for the date i.e.

DateTime::createFromFormat("Y-m-d H:i:s",$new_data['query_start_date']);

should be

$dtime = DateTime::createFromFormat("Y.m.d H:i:s",$new_data['query_start_date']);
                                     ^^^^^// Changed the date format as you've defined i.e. 2015.02.10 01:24:13

And for formatting your date you need to use format method of DateTime class like as

$post->query_start_date = $dtime->format("Y-m-d H:i:s");
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54