SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot NinjaNoodles's Avatar
    Join Date
    Oct 2006
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Christmas Conundrum

    Hello everyone,

    If you look in my signature, you'll see a link to a Christmas countdown website I've recently created. Everything works right now, but something occurred to me last night:

    I use PHP to determine if it is Christmas or not, and subsequently to determine what to display on the page (head and body). But PHP (as I'm sure you know) takes only the server's time into account. Christmas (as I'm also sure you know) doesn't occur at the same hour world-wide.

    I came up with a solution that works as follows:

    If the PHP variables are undefined, a JavaScript function is called to create them based on the client-side date and time. The values are then sent via POST back to the page and picked up by the PHP.

    Here's an excerpt:
    PHP Code:
    <!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <?php
    // Determine whether or not it's Christmas, and what year employ
    $year $_POST["xmasYear"];
    $xmas $_POST["xmas"];
    $body_class "standard"// Default page

    if ($xmas == "true") {
        
    $body_class "alternate"// Christmas version of things
    }

    if (
    $xmas == "false") {
        echo 
    "<title>Christmas Countdown ".$year."</title>\n"// Default title
    } else {
        echo 
    "<title>Merry Christmas!</title>\n"// Title on Christmas
    }
    ?>
    </head>
    <?php
    // Are the variables there?  If not, run the JavaScript
    if (empty($year) || empty($xmas)) {
    ?>
    <body onload="check_status()">
    <script type="text/javascript">
    function check_status() {

        // Setting up all the date variables
        var today = new Date();
        var currentMonth = today.getMonth()+1;
        var currentDate = today.getDate();
        var currentYear = today.getFullYear();
        var xmasYear;
        var xmas;

        // Is it Christmas?  Should we count down to 2009 or 2010?
        if ((currentMonth == 12) && (currentDate == 25)) {
            xmasYear = currentYear+1;
            xmas = "true";
        } else if ((currentMonth == 12) && (currentDate > 25)) {
            xmasYear = currentYear+1;
            xmas = "false";
        } else {
            xmasYear = currentYear;
            xmas = "false";
        }
        
        // Create the form dynamically
        var form = document.createElement("form");
        form.setAttribute("method", "post");
        form.setAttribute("action", window.location.href);

        // Create the inputs and populate them with the appropriate values
        var xmasYearField = document.createElement("input");
        xmasYearField.setAttribute("type", "hidden");
        xmasYearField.setAttribute("name", "xmasYear");
        xmasYearField.setAttribute("value", xmasYear);

        var xmasField = document.createElement("input");
        xmasField.setAttribute("type", "hidden");
        xmasField.setAttribute("name", "xmas");
        xmasField.setAttribute("value", xmas);
        
        // Send everything back to the document
        form.appendChild(xmasYearField);
        form.appendChild(xmasField);
        
        document.body.appendChild(form);
        form.submit();
    }
    </script>
    <?php
    } else {
    ?>
    <body class="<?php echo $body_class?>">
    <?php
    }
    ?>
    Of course, the variables need to be accurate and up-to-date; however, the POST data is persistent. When the page is refreshed or reloaded, this creates an obvious problem (plus an annoying warning message in the browser). With no way to clear the POST data after I've used it, I'm stuck...

    I'm trying to avoid using GET.


    Solutions?

    Thanks,
    Peter

  2. #2
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    When the post data is sent, save the information to a cookie, then redirect the user back to the page

  3. #3
    SitePoint Zealot NinjaNoodles's Avatar
    Join Date
    Oct 2006
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that was quick. I'll try it and let you know what happens...

  4. #4
    SitePoint Zealot NinjaNoodles's Avatar
    Join Date
    Oct 2006
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked perfectly. I'll have to make myself more familiar with the use of cookies.

    Thanks.

    -Peter

  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cookies are delicious, I often consume them with coffee. Some people drink milk with them, but personally that's not for me.

  6. #6
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    OH! Cookie jokes.

    Hilarious.

    Don't forget that when the xmas site countdown reaches 0, that there should be some cookies and milk left out for Santa. Make sure your cookies have not expired by the time Santa arrives or he will be [spikeyEdit]really upset[/spikeyEdit]. Same goes for the milk.
    Last edited by spikeZ; Nov 5, 2009 at 05:46. Reason: santa doesn't get p'eed off......!

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:


    Quote Originally Posted by Mal Curtis View Post
    ...Santa arrives or he will be [spikeyEdit]really upset[/spikeyEdit]. Same goes for the milk.
    rofl at Spikez edit.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

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