0

I've got a date in DD.MM.YYYY h:mm a format, how could i parse it in php or laravel to store it in a datetime field in mysql database?

Here's the date i want to parse: 29.08.2015 11:00 pm

I tried to parse to parse it like this:

date_create_from_format('DD.MM.YYYY h:mm a','29.08.2015 11:00 pm');

But it didn't work, it returns false...

Jonathan Solorzano
  • 6,812
  • 20
  • 70
  • 131
  • 2
    probably because you should investigate yourself before posting a question, and if you had done so, you would have found a thousand links that would help you already. if you then has a specific problem with your code, you could come back here (but also then, search for an answer to that problem first) – hoijui Aug 30 '15 at 05:00
  • Always, If you find any answer solved your question, mark it as correct answer. It will be helpful for those who search for the same. – Subin Thomas Aug 30 '15 at 07:37

2 Answers2

2
$date = date_create_from_format('j.m.Y h:i a','29.08.2015 11:00 pm');
echo $date->format('Y-m-j H:i:s');

Outputs 2015-08-29 23:00:00

Russell Harkins
  • 404
  • 3
  • 4
1

new DateTime(string date) converts a string to a date object without caring the format. You can pass '29-8-2015','29 Aug 2015'.. etc as $datestring. No need to define the format of input.

$datestring='29.08.2015 11:00 pm';      //defined date as string
$datetime = new DateTime($datestring);      //create datetime of defined date
$datetime = $datetime->format('Y-m-d H:i:s');   //reformat to the format we need
Subin Thomas
  • 1,408
  • 10
  • 19