Validating Time & Date To Be At Least A Certain Amount Of Time In The Future
Posted
by
MJH
on Stack Overflow
See other posts from Stack Overflow
or by MJH
Published on 2013-07-02T22:43:46Z
Indexed on
2013/07/02
23:05 UTC
Read the original article
Hit count: 268
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?
© Stack Overflow or respective owner