views:

124

answers:

9

i had two times in the format like 7:30:00 and 22:30:00 stored in the variable $resttimefrom and $resttimeto respectively

I want to check whether the current time is between this 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. Plz help

+1  A: 

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

Gordon
A: 
$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
quantumSoup
+1  A: 

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

Jonathon
As mentioned in the answers above: if the time is provided to you as a string, then it would be easier to use strtotime
Jonathon
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.
Gordon
+2  A: 

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'));
Prot0
does not take DST into account.
Gordon
A: 
    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";
        } 
A: 
$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;
+3  A: 

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";
} 
Alfwed
good answer, no need for expensive time conversions
stereofrog
A: 

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
}
emurano
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.
emurano
A: 

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

Rajasekar