18

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?

2
  • possible duplicate of PHP - Compare Date Commented Jan 24, 2013 at 17:00
  • It is because $resttimefrom and $resttimeto are not defined Commented Nov 5, 2015 at 9:57

11 Answers 11

27

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..

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for helping but to avoid error "Notice: Use of undefined constant now - assumed 'now'", encapsulate keywords into quotes
22

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"; } 

1 Comment

Good idea. I'm trying to do this in Twig without strtotime and this works nicely.
4
$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 

Comments

4

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')); 

1 Comment

does not take DST into account.
3

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

Comments

3

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"; } 

Comments

2

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); 

http://php.net/mktime

2 Comments

As mentioned in the answers above: if the time is provided to you as a string, then it would be easier to use strtotime
When called with no arguments, mktime() throws an E_STRICT notice. Also, your code could potentially lead to false results when DST is a factor. mktime has a flag to indicate DST, but as of PHP5.3 it also throws an E_DEPRECATED notice if the is_dst parameter is used.
2

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 } 

1 Comment

p.s. don't use mktime() without parameters. While this still works it is now deprecated. Use time() to get the number of seconds since epoch.
0
$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; 

Comments

0
$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; 

Comments

-6

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.