Reputation:
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??
Upvotes: 1
Views: 45
Reputation: 41875
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.
Upvotes: 1
Reputation: 9635
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');
Upvotes: 1
Reputation: 529
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
Upvotes: 0