验证时间和日期至少在未来的一定时间内

时间:2013-07-02 22:43:47

标签: php html5 forms validation datetime

我为一家出租车公司制作了一份预订表格,该表格运作良好,但我对用户预订的问题很有用,这些预订将来很快就会到期。

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

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

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

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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";
        }

再次感谢大家的帮助。