0

I can't for the life of me work out why I'm getting an HTTP 500 Error when submitting my form. I've managed to narrow it down to my PHP date formatting. But I can't seem to find the bug, I know it lies here as I've trailed and errored the entire post function commenting out function by function until the form posted.

Which has led me here:

$db_responseDate = $_POST['responseDate'];
$date2 = DateTime::createFromFormat('Y-m-d', $db_responseDate);
$db_responseDate = $date2->format('d/m/Y');

My HTML input is as follows:

<label for="responseDate">Response Date:</label>
<input type="date" class="form-control" id="responseDate" name="responseDate">

If anyone can shed some light on where I'm going wrong here, I'd really appreciate it. Thanks in advance.

  • Check your server's error log. That will tell you the exact cause of the 500 response – Phil Oct 07 '20 at 01:47
  • Just to add, if I remove the date formatting while leaving the post code in the form works. But of course the day isn't formatted. – Spud Mckenzie Oct 07 '20 at 01:48
  • I also forgot to add this only happens when the input is left empty. If I enter a date the form posts and formats the date. Thought that might help narrow the cause down. – Spud Mckenzie Oct 07 '20 at 01:53
  • Here's the error message you're not seeing ~ [_"Fatal error: Uncaught Error: Call to a member function format() on bool"_](https://3v4l.org/9IRal). – Phil Oct 07 '20 at 01:57
  • Try `$db_responseDate = !empty($_POST['responseDate']) ? $_POST['responseDate'] : date("Y-m-d");` to default to todays date if the posted data is empty. https://3v4l.org/jTbY4 – Phil Oct 07 '20 at 02:05

0 Answers0