dmikester1
dmikester1

Reputation: 1370

DateTime::createFromFormat - A two digit second could not be found

I cannot figure out how to get this PHP code to format my time for a MySQL Time format. Keeps giving me an error saying: 'A two digit second could not be found'.

Here is the PHP code:

<?php
  $timeMeeting = "6:00pm";
  $mysqlTimeMeeting = DateTime::createFromFormat('H:i:s', $timeMeeting);
  echo "time: " . $mysqlTimeMeeting . "<br />";
  var_dump(DateTime::getLastErrors());
?>

Upvotes: 0

Views: 4390

Answers (1)

John Conde
John Conde

Reputation: 219864

You have a lot of errors here. Your format is incorrect as the error message tells you. You don't have any seconds specified in $timeMeeting but you do have AM or PM. You also don't have 24 hour time, but 12 hour time and forget to format your date when you echo it out.

Basically you seem to misunderstand how DateTime::createFromFormat() works as it requires you to read the datetime in using the format the datetime is currently in (you have to tell it what that format is as it is not standard otherwise you wouldn't need to use this method) and then output it in a new format specified by you.

<?php
  $timeMeeting = "6:00pm";
  $mysqlTimeMeeting = DateTime::createFromFormat('h:ia', $timeMeeting);
  echo "time: " . $mysqlTimeMeeting->format('H:i:s') . "<br />";
  var_dump(DateTime::getLastErrors());
?>

Demo

Upvotes: 1

Related Questions