0

I have made an online booking application for a small company. Where clients can book their own appointment.

Some people helped me with testing and I found a bug the next bug in my PHP error log:

Backend fatal error: PHP Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (20/01/2022, 09:00:00) at position 0 (2)

This is my code:

$email = $_GET["email"];
$start_tijd = new DateTime(date($_GET["date"]));
$end_tijd =  new DateTime(date("Y-m-d H:i" , (strtotime($_GET["date"]) + 3600)));
if (isset($_GET["id"])) {
    $full_name = $voornaam." ".$achternaam;
    $title = "Consult ".$full_name;
}
else{
    $title = $_GET["title"];
    $full_name = substr($title, 8);
    $clientID = last_client_id();
}
$query = "
INSERT INTO events 
(title, start_event, end_event, clientID, locatie) 
VALUES (:title, :start_event, :end_event, :clientID, :type)
";
$statement = $connect->prepare($query);
$statement->execute(
array(
':title'  => $title,
':start_event' => $start_tijd->format('Y-m-d H:i:s'),
':end_event' => $end_tijd->format('Y-m-d H:i:s'),
':clientID' => $clientID,
':type' => $_GET["type"]
)
);
send_appointment_email($email, $full_name, $start_tijd->format('d-m-Y H:i:s'), $end_tijd->format('H:i:s'), $clientID);

About 25 people have booked an appointment and 2 people got the bug.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    I doubt it can parse 20/01/2022 because its d/m/y rather than the default m/d/y or the universal y/m/d. Use DateTime::createFromFormat if you want to support other date formats in the input data – ADyson Dec 30 '21 at 23:51
  • Also, please remember to [properly indent](/markdown) you code =) – Mike 'Pomax' Kamermans Dec 30 '21 at 23:51
  • You should not be using `strtotime` or `date`. Just use `DateTime` – Dharman Dec 30 '21 at 23:59
  • @ADyson Thanks for your feedback I am gonna change the date format. But I don't get why the bug appears not always. – Thomas Alkmaar Dec 30 '21 at 23:59
  • Can you tell what date this is? `01/02/2022`? Without knowing the format PHP can't guess accurately – Dharman Dec 30 '21 at 23:59
  • @Dharman Thanks for your feedback, but how do I add an hour to datetime? I used that because I found this: https://stackoverflow.com/questions/11386308/add-x-number-of-hours-to-date/17942233 – Thomas Alkmaar Dec 31 '21 at 00:03
  • In the post you linked the second answer shows how to do it https://stackoverflow.com/a/23103855/1839439 You can also read from PHP manual https://www.php.net/manual/en/datetime.modify.php – Dharman Dec 31 '21 at 00:05
  • @Dharman 01/02/2022 is a date a client picked on a jquery calendar. But in my country we use the notation d/m/y – Thomas Alkmaar Dec 31 '21 at 00:11
  • I don't know how your UI looks like. Your job is to ensure that users provide the date in the correct format every time. Usually we use ISO 8601 format for this. If you think they will always give you date in format `d/m/y` you might be wrong. – Dharman Dec 31 '21 at 00:13
  • @Dharman I have a textbox where the user can see the selected date. The date from the textbox is from the datepicker and the user is not able to typ in the textbox. And I am going to look at the manual and the second answer. – Thomas Alkmaar Dec 31 '21 at 00:23

0 Answers0