0

我已经为一家出租车公司建立了一个可以正常工作的预订表格,但是我遇到了一个问题,即用户预订未来过早到期的预订。

由于整个表格有点长,我首先要确保用户不会尝试提前不到一个小时进行预订,而不必填写整个表格。

到目前为止,这是我想出的,但它不起作用:

    <?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>

如何创建验证检查以确保预订的日期和时间至少提前一小时?

4

2 回答 2

1

如果你这样做(添加回显线)你会看到一个大问题,你混合了 2 种不同的日期类型!
echo "current: $current_time Event: $event_time";
if (($current_time - $event_time) <= (3600))

于 2013-07-02T23:11:22.703 回答
0

好吧,我想通了。这是我想出的:

    <?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";
        }

再次感谢大家的帮助。

于 2013-07-03T16:43:14.950 回答