Reputation: 174
I'm currently working on a program in PHP that will display a button for a subject . But I don't know the algorithm for if the schedule is 15 minutes before it starts.
eg. the subject will start 2:30 PM. The system will compare the current time, if the current time is 2:15 PM (15 minutes before the subject will start) the clickable button will display, else it shows a message for the time remaining.
$current_time = date('h:i A');
$time_start = date('h:i A', strtotime($r['time_start']));
$time_end = date('h:i A', strtotime($r['time_end']));
if( $current_time is 15 mins before $time_start && $current_time < $time_end){
//show clickable button
}else{
//show time remaining
}
Please help, the logic/algorithm is confusing me
Upvotes: 0
Views: 120
Reputation: 41968
Don't compare strings, compare timestamps. time()
is your friend.
$diff = time() - strtotime($r['time_start']);
if($diff < 0)
..too late, exam has started
elseif($diff > 15*60)
..more than 15 minutes remaining
else
..between 15 minutes and on time
Using DateTime
would also work but is just major overkill for simply comparing 2 basic timestamps.
Upvotes: 2
Reputation: 767
$time = time()
if(($time > strtotime("2:15pm")) && ($time < strtotime("2:30pm")){
//Display button
}else{
//Show time remaining
}
Upvotes: -1
Reputation: 6319
This is where Datetime objects become really handy:
$time_start = new DateTime($r['time_start']);
$now = new DateTime();
$diff = $time_start->diff($now);
if($diff->i < 15){
// Do stuff
}
Upvotes: 2