$startTime = str_replace(":", "", 11:30); $endTime = str_replace(":", "", 16:00); $currentTime = str_replace(":", "", date('H:i')); if ($currentTime >= $startTime) { $buttons = '<a href="javascript:void(0)" class="btn btn-success btn-sm">Start Exam</a>'; } else if($currentTime < $startTime) { $buttons = '<a href="javascript:void(0)" class="btn btn-info btn-sm">Please wait till start time..</a>'; } else if($currentTime <= $endTime) { $buttons = '<a href="javascript:void(0)" class="btn btn-danger btn-sm">Exam Over</a>'; }
In the above code. What happen now when start time and current time
is equal or current time
is greater then start time
then it show start exam
but it not working properly. So, How can I compare start and end time with current time? Please help.
Thank you
Advertisement
Answer
You need to initialize dates variables as DateTime and set the time. For example this
$startTime = (new DateTime())->setTime(11, 30,0); $endTime = (new DateTime())->setTime(16, 0, 0); $currentTime = new DateTime(); if($currentTime >= $endTime ) { $buttons = '<a href="javascript:void(0)" class="btn btn-danger btn-sm">Exam Over</a>'; } else if ($currentTime >= $startTime) { $buttons = '<a href="javascript:void(0)" class="btn btn-success btn-sm">Start Exam</a>'; } else if($currentTime < $startTime) { $buttons = '<a href="javascript:void(0)" class="btn btn-info btn-sm">Please wait till start time..</a>'; }
Here is a full playground https://tehplayground.com/O8vPv3ZExM4U7AtY