Reputation: 238
Let's say there are two given times and an interval. I can check if the time is between two given times like so:
$timeA = '00:30:00';
$timeB = '14:00:00';
$interval = 30; // minutes
$inputTime = '00:45:00'; // user inputted time
$input = new DateTime($inputTime);
$begin = new DateTime($timeA);
$end = new DateTime($timeB);
if($input >= $begin && $input <= $end) {
echo 'Success';
} else {
echo 'Fail';
}
But how can I check for the interval? For example, the interval is 30 minutes so only show success message if $inputTime
is between $begin
and $end
AND one of the following:
00:30:00
01:00:00
01:30:00
02:00:00
02:30:00
03:00:00
03:30:00
04:00:00
04:30:00
...
13:30:00
14:00:00
and if it does not fall on one of the "interval" times, then it fails.
Upvotes: 0
Views: 1032
Reputation:
You can use strtotime()
to convert to integer time and then find the difference. Then compare the difference between that time and 30 minutes.
Upvotes: 0
Reputation: 2359
You can loop through the times in between the two given times and if it lands on an "interval" time, you can break from the for
loop and echo out a success message.
For example:
$found = false;
for ($i=0; $i < 1440; $i++) {
$begin->add(new DateInterval('PT' . $interval . 'M'));
if($begin == $input) {
$found = true;
break;
} elseif($begin > $input || $begin > $end) {
$found = false;
break;
}
}
if($found) {
echo 'Success';
} else {
echo 'Fail';
}
Upvotes: 1