1

I have the following function

function status($open, $lunch, $close)
{
 if(date('H') < $open || date('H') > $close)
 {
  $GLOBALS['status'] = "Closed";
  $GLOBALS['color'] = "rgba(255,0,0,1)";
 }
 elseif(date('H') == ($close-1))
 {
  if(date('i') > 29)
  {
   $GLOBALS['status'] = "Closing";
   $GLOBALS['color'] = "rgba(255,255,0,1)";
  }
  else
  {
   $GLOBALS['status'] = "Open";
   $GLOBALS['color'] = "rgba(0,255,0,1)";
  }
 }
 else
 {
  if(date('H') == $lunch)
  {
   $GLOBALS['status'] = "Lunch";
   $GLOBALS['color'] = "rgba(0,0,255,1)";
  }
  else
  {
   $GLOBALS['status'] = "Open";
   $GLOBALS['color'] = "green";
  }
 }
}

and after 10:00PM it is supposed to return the status of closed and right now its 10:13 and it's still returning open, I've gone through the code and cannot seem to find the problem.

Could someone take a look and see where my code is failing??

5
  • 10:00PM, you inputted 22 on closing? Commented Aug 30, 2014 at 5:19
  • when I call the function it is written status(10,12,22). but for some reason it is registering the final else statement. Commented Aug 30, 2014 at 5:21
  • what is the time zone your php server is in? Might need to make adjustment to time zone or daylight savings time. Commented Aug 30, 2014 at 5:22
  • @GaryHayes The timezone is correct and when I have the server return the time it returns the time I am expecting Commented Aug 30, 2014 at 5:24
  • Looking like a problem with your nested if else statements Commented Aug 30, 2014 at 5:25

3 Answers 3

1

Try using >= on closing.

if(date('H') < $open || date('H') >= $close)
{
    $GLOBALS['status'] = "Closed";
    $GLOBALS['color'] = "rgba(255,0,0,1)";
}

What happened is you set 10:00 PM which is 22. If the current time is 10:13 which is still not greater than 22. That's why it did fail the condition. Should be greater than or equal, then, it's closed.

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

1 Comment

when I originally wrote the script I had put the equals sign in, but when I edited it this afternoon, I must have removed it. Thank you for pointing out my mistake
1

try this

function status($open, $lunch, $close)
{
    $hour = date('H');
    $minute = date('i');

    if($hour < $open ||  $hour >= $close)
    {
        $GLOBALS['status'] = "Closed";
        $GLOBALS['color'] = "rgba(255,0,0,1)";
    }
    else if($hour==$lunch)
    {
        $GLOBALS['status'] = "Lunch";
        $GLOBALS['color'] = "rgba(0,0,255,1)";
    }
    else
    {
        $GLOBALS['status'] = "Open";
        $GLOBALS['color'] = "green";
    }

    if($hour == ($close-1) && $minute>29)
    {
        $GLOBALS['status'] = "Closing";
        $GLOBALS['color'] = "rgba(255,255,0,1)";
    }
}

NOTE : also set your default timezone like

date_default_timezone_set('your timezone');
//example
date_default_timezone_set('Asia/Kolkata');

Comments

0

Make sure you have the correct time zone set,

date_default_timezone_set('Asia/Colombo');

List of timezones supported in php can be found here

1 Comment

timezone is set to America/Vancouver in my .user.ini file on the server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.