Jump to content

Recommended Posts

Posted

Hi All,

 

For a while now I've been running a PHP page as part of my Xibo setup which showed the current period in the day. I get the orginal code from this forum but over time I've had a lot of problems with it. Today I noticed its now working correctly again. Please could keen PHP coders take a look at tell me why 'Break' is appearing with 'Period 3' from 11:20 to 12:00. The break text should not be shown from 11:20 onwards. I outputted the $hour and $minute so I know the server time is as it should be.

 

$hour=Date("G");
$minute=Date("i");


if (($hour < "8" && $hour == "8") || ($hour == "8" && $minute <= "50"))
{
echo("Before School");
}
if (($hour == "8" && $minute > "50") || ($hour == "9" && $minute < "5"))
{
echo("Registration");
}
if (($hour == "9" && $minute > "5") || ($hour == "10" && $minute < "5"))
{
echo("Period 1");
}
if (($hour == "10" && $minute > "5") || ($hour == "11" && $minute < "5"))
{
echo("Period 2");
}
if (($hour == "11" && $minute > "5") || ($hour == "11" && $minute < "20"))
{ 
echo("Break");
}
if (($hour == "11" && $minute > "20") || ($hour == "12" && $minute < "20"))
{
echo("Period 3");
}
if (($hour == "12" && $minute > "20") || ($hour == "13" && $minute < "20"))
{
echo("Lunch");
} 
if (($hour == "13" && $minute > "15") || ($hour == "14" && $minute < "15"))
{
echo("Period 4");
} 
if (($hour == "14" && $minute > "15") || ($hour == "15" && $minute < "15"))
{
echo("Period 5");
} 
if (($hour == "15" && $minute > "15") || ($hour > "16" && $minute < "30"))
{
echo("After School");
} 

 

I think its ignoring the minute condition, but all the others seem to work fine so not sure what's going on.

 

Thank You, Rob

Posted

You need both conditions to be correct so should use and instead of or for the two time conditions.

 

For break the time should be after 11:05 and before 11:20.

Posted

Personally, I would do something like

 

if (time() > strtotime("09:05") && time() <= strtotime("10:05")) {echo "Period 1";}

etc

 

You will most likely warnings about TZ but you can either turn off warnings or include

date_default_timezone_set('Europe/London');

Posted
Personally, I would do something like

 

if (time() > strtotime("09:05") && time() <= strtotime("10:05")) {echo "Period 1";}

etc

 

You will most likely warnings about TZ but you can either turn off warnings or include

date_default_timezone_set('Europe/London');

 

Hi @unixman_again

 

I've tried to put $time=Date and get a warning about 'Use of undefined constant date' which is guess is what you mean. However including the above results in a PHP error.

Posted

As @yorkie said, you need to change your or's to and's.

 

[color=#007700][font=monospace]if (([/font][/color][color=#0000BB][font=monospace]$hour [/font][/color][color=#007700][font=monospace]== [/font][/color][color=#DD0000][font=monospace]"11" [/font][/color][color=#007700][font=monospace]&& [/font][/color][color=#0000BB][font=monospace]$minute [/font][/color][color=#007700][font=monospace]> [/font][/color][color=#DD0000][font=monospace]"5"[/font][/color][color=#007700][font=monospace]) && ([/font][/color][color=#0000BB][font=monospace]$hour [/font][/color][color=#007700][font=monospace]== [/font][/color][color=#DD0000][font=monospace]"11" [/font][/color][color=#007700][font=monospace]&& [/font][/color][color=#0000BB][font=monospace]$minute [/font][/color][color=#007700][font=monospace]< [/font][/color][color=#DD0000][font=monospace]"20"[/font][/color][color=#007700][font=monospace]))

This means it will only show when it is between 11:05-11:20 rather than after 11:05 or before 11:20 (which would catch all of 11:00-11:59)[/font][/color]

Posted
As @yorkie said, you need to change your or's to and's.

 

Hi @Katy,

 

Thanks, after a bit of trail and error I think I've got it working. I need && when the hour is the same on each side, but || when the hour is different. I'll keep an eye on it today and see if it works correctly.

 

 date_default_timezone_set('Europe/London');
echo('');

$hour=Date("G");
$minute=Date("i");

if (($hour == "8" && $hour == "8") && ($hour == "8" && $minute <= "50"))
{
echo("Before School");
}
if (($hour == "8" && $minute > "50") && ($hour == "9" && $minute < "5"))
{
echo("Registration");
}
if (($hour == "9" && $minute > "5") || ($hour == "10" && $minute < "5"))
{
echo("Period 1");
}
if (($hour == "10" && $minute > "5") || ($hour == "11" && $minute < "5"))
{
echo("Period 2");
}
if (($hour == "11" && $minute > "5") && ($hour == "11" && $minute < "20"))
{ 
echo("Break");
}
if (($hour == "11" && $minute > "20") || ($hour == "12" && $minute < "20"))
{
echo("Period 3");
}
if (($hour == "12" && $minute > "20") || ($hour == "13" && $minute < "20"))
{
echo("Lunch");
} 
if (($hour == "13" && $minute > "15") || ($hour == "14" && $minute < "15"))
{
echo("Period 4");
} 
if (($hour == "14" && $minute > "15") || ($hour == "15" && $minute < "15"))
{
echo("Period 5");
} 
if (($hour == "15" && $minute > "15") || ($hour > "16" && $minute < "30"))
{
echo("After School");
} 


echo('');
?> 

 

Thank you all!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...