Current time between two hours

97 views Asked by At

My site has "StartHour" and "EndHour"

Example:

If the current time (1500) is between the StartHour(0800) and EndHour(2300) the site works, then:

    $currentTime= date('Hi');
    $startTime= $setting_info['start_hour']; // 
    $endTime=  $setting_info['end_hour']; //
if($currentTime>=$startTime && $currentTime<=$endTime) 
    {
    echo "Site Works Perfectly";
    }

The problem is when the $endTime is bigger than 2359 for example, I want that my site works from 0800 to 0200. It won't work because if $currenttime is 1000 it will be bigger than $startTime but will be bigger than $endTime too.

Or if $currenttime is 0000 it will be smaller than $starttime and smaller than $endtime.

I can't figure out how to solve this.

Please help!

2

There are 2 answers

0
Kunal Pradhan On

Use it like below:

<?php
 $currentTime= time();
$startTime= "08:00:00";
$endTime=  "22:00:00";
 $startTime=strtotime(date('Y-m-d '.$startTime));
 strtotime($startTime);
$endTime=strtotime(date('Y-m-d '.$endTime));
if($currentTime>=$startTime && $currentTime<=$endTime) 
    {
    echo "Site Works Perfectly";
    }
?>
0
Allkin On

You can convert the Time Strings to timestamps with strtotime() and compare them with thime()

$st_time    =   strtotime($setting_info['start_hour']);
$end_time   =   strtotime($setting_info['end_hour']);
$cur_time   =   strtotime(now);

if($st_time < $cur_time && $end_time > $cur_time)
{
   echo "WE ARE CLOSE NOW !!";
}
else{
   echo "WE ARE OPEN  NOW !!";
}