Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    204
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Date Script going crazy

    Can anyone tell me why this as started to randomly display the wrong dates.
    ( Shortened version but you get the idea )

    PHP Code:
    <?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
    ?>
    Last edited by Feckie; 08-01-2012 at 09:11 PM.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    i'm surprised that actually works without curly brackets...

    PHP Code:
    <?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 
    ?>

  • Users who have thanked Arcticwarrio for this post:

    Feckie (08-01-2012)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • The Following 2 Users Say Thank You to Fou-Lu For This Useful Post:

    Arcticwarrio (08-01-2012), Feckie (08-01-2012)

  • #4
    Regular Coder
    Join Date
    Jan 2009
    Posts
    204
    Thanks
    29
    Thanked 0 Times in 0 Posts
    I have added braces, and it seems to be working ok again.

    Time will tell

    Thanks

  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    I am surprised that this is fixed with brackets, and thought it more likely that the default timezone hasn't been set:

    PHP Code:
    date_default_timezone_set('America/Los_Angeles'); 
    But, hey!
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by AndrewGSW View Post
    I am surprised that this is fixed with brackets, and thought it more likely that the default timezone hasn't been set:

    PHP Code:
    date_default_timezone_set('America/Los_Angeles'); 
    But, hey!
    My suspicion is that the "shortened version" was multiple instructions per block.

  • #7
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    My suspicion is that the "shortened version" was multiple instructions per block.
    That makes more sense . I understand it's advisable to always set the timezone though?
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #8
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    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 Code:
    <?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
    }
    ?>
    Leonard Whistler

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by AndrewGSW View Post
    That makes more sense . 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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •