0

I've built a reservation form for a taxi company which works fine, but I'm having an issue with users making reservations that are due too soon in the future.

Since the entire form is kind of long, I first want to make sure the user is not trying to make a reservation for less than an hour ahead of time, without them having to fill out the whole form.

This is what I have come up with so far, but it's just not working:

    <?php
//Set local time zone.
date_default_timezone_set('America/New_York');
//Get current date and time.
$current_time = date('Y-m-d H:i:s');
//Set reservation time variable
$res_datetime = $_POST['res_datetime'];
//Set event time.
$event_time = strtotime($res_datetime);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Check Date and Time</title>
</head>
<?php
//Check to be sure reservation time is at least one hour in the future.
if (($current_time - $event_time) <= (3600))
{
    echo "You must make a reservation at least one hour ahead of time.";
}
?>
<form name="datetime" action="" method="post">
<input name="res_datetime" type="datetime-local" id="res_datetime">
<input type="submit">
</form>
<body>
</body>
</html>

How can I create a validation check to make sure the date and time of the reservation is at least one hour ahead of time?

AnarchyOutlaw
  • 163
  • 2
  • 6
  • Event time - current time > 3600. – Ja͢ck Jul 02 '13 at 22:58
  • There are a number of things wrong here. I suggest you take a look at a good Forms tutorial like [W3Schools](http://www.w3schools.com/php/php_forms.asp) to sort out the form handling before you start on validation. When you get to that point, use Unix timestamps for your dates and then checking the difference is greater than 3600 will do what you need. –  Jul 02 '13 at 23:03
  • No, for _the God of DST’s_ sake, don’t add what you _think_ would _always_ mean “one hour” – use `strtotime('+1 hour')` (and compare it to `time()`) instead. – CBroe Jul 02 '13 at 23:07
  • There are a few things going on as other people have pointed out - date() produces a string whereas strtotime() produces an integer, your comparison is wrong, and your form-action should probably be filled out as well. – dethtron5000 Jul 02 '13 at 23:11
  • I appreciate your feedback. I'm gonna try to edit things according to your suggestions. I have read the W3Schools tutorial, but it was really basic. It doesn't really teach you how to do anything. I've also looked at the PHP Manual. Whenever I Google PHP questions this site comes up, so I thought I would ask here. – AnarchyOutlaw Jul 02 '13 at 23:23
  • This is basically what I was looking for: [link]http://stackoverflow.com/questions/660501/simplest-way-to-increment-a-date-in-php/660521#660521 – AnarchyOutlaw Jul 03 '13 at 15:51
  • If you really want to make things easier for the user, I would also add javascript validation for the time entered, that way you could display an error message and/or prevent form submittal before you even need to validate on the server side (note this should not replace server-side validation). – Mike Brant Jul 03 '13 at 17:17

2 Answers2

1

If you do this (add the echo line) you will see a big problem, you are mixing 2 different date types!
echo "current: $current_time Event: $event_time";
if (($current_time - $event_time) <= (3600))

Joe T
  • 2,300
  • 1
  • 19
  • 31
0

Okay, I figured it out. Here's what I came up with:

    <?php
//Set time zone:
        date_default_timezone_set('America/New_York');
//Set current time:
        $current_time = date("Y-m-d H:i:s");
//Convert current time to timestamp:
        $current_timestamp = strtotime($current_time);
//Set the minimum amount of time in advance a reservation can be made:
        $min_time = strtotime("+1 hour", strtotime($current_time));
//Set event date:
        $event_date = $_POST['event_date'];
//Set event time:
        $event_time = $_POST['event_time'];
//Convert event time to timestamp:
        $event_timestamp = strtotime("$event_date . $event_time");
//Set availability:
    //If required fields are set and validated:
    if (isset($event_date, $event_time)) {
        //If reservation time is made in the past:
        if ($event_timestamp < $min_time && $event_timestamp < $current_timestamp) {
        $availability = "in_the_past";
        //If reservation time is less than the required amount of time in advance:
        } elseif ($event_timestamp > $current_time && $event_timestamp < $min_time) {
        $availability = "too_soon";
        //If a reservation is made for when no one may be checking email in time:
        } elseif ($event_timestamp > strtotime('today 8:00pm') && $event_timestamp < strtotime('tomorrow 6:00am')) {
        $availability = "unavailable";
        } else {
        $availability = "available";
        }

Thanks again for everyone's help.

AnarchyOutlaw
  • 163
  • 2
  • 6