Mysql table for hours(field type time
):
opened closed
12:00:00 23:59:00
In php i need to check if the current time is between these two times.
FIrst i have converted php current server time into mysql time format
$cur_time = date('H:m A',$_SERVER['REQUEST_TIME']);
$cur_time = DateTime::createFromFormat( 'H:i A',$cur_time);
$cur_time = $cur_time->format('H:i:s');
Then i have compared the times-
if($cur_time > $biz_hours['opened'] && $cur_time < $biz_hours['closed'])
Note: $biz_hours
is the array to fetch data from mysql table.
Printing the $cur_time
is displaying 09:12:00 just now. But if caluse returning false. Here is the live site url: http://dsbangladesh.com/takeout-banani/b/10 . Please help me to find the problem.
The comparison you are using is a simple string comparison. Your best bet is to convert your data into timestamps first. Something like this:
$curTime = time();
$openTime = strtotime($biz_hours['opened']);
$closeTime = strtotime($biz_hours['closed']);
if($curTime > $openTime && $curTime < $closeTime)
{ ....
I've used time
to get the current server time instead of the method you used. I've also used strtotime
to convert the mysql time field into a timestamp.
if (time() > strtotime($biz_hours['opened']) && time() < strtotime($biz_hours['closed']))
<?php
$biz_o = new DateTime(date('H:i:s',strtotime($biz_hours['opened'])));
$biz_c = new DateTime(date('H:i:s',strtotime($biz_hours['closed'])));
$now = new DateTime(date('H:i:s'),strtotime($_SERVER['REQUEST_TIME']));
if($now > $biz_o && $now < $biz_c){
//You got me
}