Reputation: 4622
I'm retrieving records from a database that have a start time and an end time. I then need to compare them to see if a predefined appointment timeslot falls within the start/end times. For example I will retrieve a record from the database and have:
$recordStartTime = 9.00am
$recordStartTime = 10.30am
I then need to compare this timeframe to a predefined appointment timeslot to see if it is available or not - these predefined appointment timeslots are in 15 minute increments. So if I have an appointment timeslot from 9.30am - 9.45am using the above example this would be considered "unavailable". If I had an appointment timeslot of 2.00pm - 2.15pm this would be considered "available".
I will be looping through an array of records to do the comparisons.
Upvotes: 0
Views: 446
Reputation: 8819
if you are looking to find out the time between the slot than you can check the code below.
$checkCurrentTime = "10:45 am";
$recordStartTime = "9:00 am";
$recordEndTime = "10:30 am";
$date1 = DateTime::createFromFormat('H:i a', $checkCurrentTime);
$date2 = DateTime::createFromFormat('H:i a', $recordStartTime);
$date3 = DateTime::createFromFormat('H:i a', $recordEndTime);
if ($date1 > $date2 && $date1 < $date3){
echo 'slot not availble';
}else{
echo 'slot availble';
}
Upvotes: 0
Reputation: 480
I'm not sure with that kind of time format.. maybe it is best if you first convert it on something that can be easily read by php.
try using strtotime() or do some manual conversion.
Upvotes: 0
Reputation: 1704
You need to use strtotime(). This will convert the time in to the number of seconds that has passed since 1970-01-01 00:00:00
You can try the below.
<?php
$time1 = strtotime("9.00am");
$time2 = strototime("10.30am");
?>
$time1 and $time2 will now have numeric values which you can use to compare with normal operators.
Upvotes: 1