Tutorial :How to compare two time in PHP


I had two times in the format like 7:30:00 and 22:30:00 stored in the variables $resttimefrom and $resttimeto respectively.

I want to check whether the current time is between these two values. I am checking this with the code

$time = date("G:i:s");  if ($time > $resttimefrom and $time < $resttimeto ){      $stat = "open";  } else {      $stat = "close";  }   

But I am always getting the $stat as Close. What may cause that?


A simple yet smart way to do this is to remove the ':' from your dates.

$resttimefrom = 73000;  $resttimeto = 223000;    $currentTime = (int) date('Gis');    if ($currentTime > $resttimefrom && $currentTime < $resttimeto )  {      $stat="open";  }  else  {      $stat="close";  }   


you can try using strtotime

$st_time    =   strtotime($resttimefrom);  $end_time   =   strtotime($resttimeto);  $cur_time   =   strtotime(now);  

then check

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

i hope this may help you..


Try reformatting them into something that you can compare like that. For example, numbers:

$resttimefrom = mktime(7,30,0);  $resttimeto = mktime(22,30,0);    $time = mktime(date('H'),date('i'),date('s'));  


You are comparing strings.
Convert the Time Strings to timestamps with strtotime().
Then compare against time().


$today = date("m-d-y ");  $now = date("m-d-y G:i:s");    if (strtotime($today . $resttimefrom) < $now && $now > strtotime($today . $resttimeto)) {      $stat = 'open';  else      $stat = 'close  


Just convert your dates to a Unix Timestamp, compare them, you have your results! It might look something like this:

$time =date("G:i:s");    $time1 = strtotime($time);   $resttimefrom1 = strtotime($resttimefrom );  $resttimeto1 = strtotime($resttimeto );  if ($time1 >$resttimefrom  and $time1 <$resttimeto)  {      $stat="open";  }  else  {      $stat="close";  }   


The date function returns a string, so the comparison you're making would be a string comparison - so 7:30 would be more than 22:30

It would be much better to use mktime, which will return a Unix timestamp value (integer) so it would make for a better comparison

$currentTime = mktime();  $resttimefrom = mktime(hour,min,second);  



The trick to manipulating and comparing dates and times in PHP is to store date/time values in an integer variable and to use the mktime(), date() and strtotime() functions. The integer repesentation of a date/time is the number of seconds since midnight, 1970-Jan-1, which is referred to as the 'epoch'. Once your date/time is in integer form you'll be able to efficiently compare it to other dates that are also in integer form.

Of course since you'll most likely be receiving date/time values from page requests and database select queries you'll need to convert your date/time string into an integer before you can do any comparison or arithmetic.

Assuming you are sure that the $resttimefrom and $resttimeto variables contain properly formatted time you can use the strtotime() function to convert your string time into an integer. strtotime() takes a string that is formatted as a date and converts it to the number of seconds since epoch.

$time_from = strtotime($resttimefrom);  $time_to = strtotime($resttimeto);  

Side note: strtotime() always returns a full date in integer form. If your string doesn't have a date, only a time, strtotime() return today's date along with the time you gave in the string. This is not important to you, though, because the two dates returned by strtotime() will have the same date and comparing the two variables will have the desired effect of comparing the two times as the dates cancel each other out.

When you compare the two integers keep in mind that the earlier the date/time is, the smaller its integer value will be. So if you want to see if $time_from is earlier than $time_to, you would have this:

if ($time_from < $time_to)  {      // $time_from is ealier than $time_to  }  

Now to compare a date/time with the current system date/time, just use mktime() with no parameters to represent the current date/time:

if ($time_from < mktime())  {      // $time_from is in the past  }  


$firstTime = '1:07';  $secondTime = '3:01';    list($firstMinutes, $firstSeconds) = explode(':', $firstTime);  list($secondMinutes, $secondSeconds) = explode(':', $secondTime);    $firstSeconds += ($firstMinutes * 60);  $secondSeconds += ($secondMinutes * 60);  $difference = $secondSeconds - $firstSeconds;  


$Time1 = date_parse($time);  $seconds1 = $Time1['hour'] * 3600 + $Time1['minute'] * 60 + $Time1['second'];    $Time2 = date_parse($current_time);  $seconds2 = Time2['hour'] * 3600 + Time2['minute'] * 60 + Time2['second'];    $actula_time = $seconds1 - $seconds2;    echo floor($actula_time / 3600) .":". floor(($actula_time / 60)%60) .":".  $actula_time%60;  


As Col. Shrapnel Said i am doing by converting all the time in to seconds and then compare it with current time's total seconds

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »