...

View Full Version : Resolved Date Script going crazy



Feckie
08-01-2012, 06:49 PM
Can anyone tell me why this as started to randomly display the wrong dates.
( Shortened version but you get the idea )


<?php
$d=date('m.d');
if ($d=="01.01")
include "/home/XXXXX/public_html/Web/Backgrounds/New-Year.php"; //New Years Day
elseif ($d=="02.02")
include "/home/XXXXX/public_html/Web/Backgrounds/GroundHogDay.php"; //Ground Hog Day
elseif ($d=="02.14")
include "/home/XXXXX/public_html/Web/Backgrounds/Valentine.php"; //Valentine Day
elseif ($d=="03.01")
include "/home/XXXXX/public_html/Web/Backgrounds/StDavidsDay.php"; //St David's Day
elseif ($d=="03.17")
include "/home/XXXXX/public_html/Web/Backgrounds/StPatricks.php"; //St. Patrick's Day
elseif ($d=="04.01")
include "/home/XXXXX/public_html/Web/Backgrounds/AprilFool.php"; //Aprils Fools Day
elseif ($d=="04.22")
include "/home/XXXXX/public_html/Web/Backgrounds/World-Earth-Day.php"; //World-Earth-Day
elseif ($d=="05.18")
include "/home/XXXXX/public_html/Web/Backgrounds/Armed-Forces-US"; //Armed-Forces-US
elseif ($d=="06.05")
include "/home/XXXXX/public_html/Web/Backgrounds/StGeorge.php"; //St George's Day
elseif ($d=="05.01")
include "/home/XXXXX/public_html/Web/Backgrounds/MayDay-UK.php"; //May Day UK
elseif ($d=="05.27")
include "/home/XXXXX/public_html/Web/Backgrounds/Memorial-Day.php"; //Memorial Day USA
elseif ($d=="06.27")
include "/home/XXXXX/public_html/Web/Backgrounds/Armed-Forces-UK.php"; //Armed-Forces-UK
elseif ($d=="07.04")
include "/home/XXXXX/public_html/Web/Backgrounds/Independance-Day.php"; //USA Independance Day
elseif ($d=="07.24")
include "/home/XXXXX/public_html/Web/Backgrounds/Amelia_Earhart.php"; //Amelia_Earhart BirthDay
else
include "/home/XXXXX/public_html/Web/Backgrounds/Main.php"; //Main Page
?>

Arcticwarrio
08-01-2012, 08:40 PM
i'm surprised that actually works without curly brackets...


<?php
$d=date('m.d');
if ($d=="01.01") {include "/home/XXXXX/public_html/Web/Backgrounds/New-Year.php";} //New Years Day
elseif ($d=="02.02") {include "/home/XXXXX/public_html/Web/Backgrounds/GroundHogDay.php";} //Ground Hog Day
elseif ($d=="02.14") {include "/home/XXXXX/public_html/Web/Backgrounds/Valentine.php";} //Valentine Day
//...
else {include "/home/XXXXX/public_html/Web/Backgrounds/Main.php";} //Main Page
?>

Fou-Lu
08-01-2012, 08:50 PM
Braces are only required if you have instructions that are more than one line long. I always use braces.

This appears to me that it would work fine. You need to be more specific when you state randomly wrong dates. The date() function without a second parameter would assume todays date, although it would be affected by the timezone as well so you can be out as much as 24 hours without specifying the timezone.

BTW, "shortened version" is often a big cause for error since it doesn't necessarily reflect the issue.

Feckie
08-01-2012, 10:11 PM
I have added braces, and it seems to be working ok again.

Time will tell :)

Thanks

AndrewGSW
08-01-2012, 10:29 PM
I am surprised that this is fixed with brackets, and thought it more likely that the default timezone hasn't been set:


date_default_timezone_set('America/Los_Angeles');
But, hey!

Fou-Lu
08-01-2012, 10:34 PM
I am surprised that this is fixed with brackets, and thought it more likely that the default timezone hasn't been set:


date_default_timezone_set('America/Los_Angeles');
But, hey!

My suspicion is that the "shortened version" was multiple instructions per block.

AndrewGSW
08-01-2012, 10:37 PM
My suspicion is that the "shortened version" was multiple instructions per block.

That makes more sense :thumbsup:. I understand it's advisable to always set the timezone though?

Len Whistler
08-01-2012, 10:56 PM
I would consider using the switch statement and a function with the path to the file. With the function you can change the path with one edit.



<?php
function path_to_file ($file) {
include "/home/XXXXX/public_html/Web/Backgrounds/$file";
return;
}

$d=date('m.d');
switch ($d)
{
case 01.01:
path_to_file("New-Year.php"); //New Years Day
break;
case 02.02:
path_to_file("GroundHogDay.php"); //Ground Hog Day
break;
case 02.14:
path_to_file("Valentine.php"); //Valentine Day
break;
case 03.01:
path_to_file("StDavidsDay.php"); //St David's Day
break;
case 03.17:
path_to_file("StPatricks.php"); //St. Patrick's Day
break;

default:
path_to_file("Main.php"); //Main Page
}
?>

Fou-Lu
08-01-2012, 10:58 PM
That makes more sense :thumbsup:. I understand it's advisable to always set the timezone though?

Yes. Failing to set the timezone in either the ini or inline of the script will trigger an E_NOTICE indicating that the TZ environment variable should be set.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum