PHP script to schedule images.

Discussion in 'JavaScript' started by Kmonie360, Nov 25, 2012.

  1. #1
    Eventually I'd like a system where I can schedule events, but I'm using this JavaScript right now but I'd like to be using inline php. Hoping someone could help me write it correctly for in the body of the document. Thanks!

    <script type="text/javascript">var date=new Date();
    var year=date.getFullYear();
    var month=date.getMonth();
    var day=date.getDate(); // fixed
    function SetDivContent() {
        var div=document.getElementById('date_dependent');
        if (year==2012 && month==10) { // fixed (the JavaScript months order is 0-11, not 1-12)
            if (day>=6 && day<8) { // the following content will be displayed 12/03/2010, 12/04/2010, [...], 12/09/2010, 12/10/2010
                div.innerHTML='images/ud-calendar.png';
            }
            else if (day==9 || day==13) { // this one will be displayed 12/11/2010 and 12/12/2010
                div.innerHTML='images/ud-calendar.png';
            }
            else if (day>13) { // this one - 12/13/2010 and later, until the end of December
                div.innerHTML='images/ud-calendar.png';
            }
        }
        else if (year==2013 && month>=0) div.innerHTML='images/ud-calendar.png'; // OPTIONAL - just to ensure that content 3 is displayed even after December.
    }
    </script>
    Code (markup):

     
    Kmonie360, Nov 25, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    echo '<script type="text/javascript">var date=new Date();
    var year=date.getFullYear();
    var month=date.getMonth();
    var day=date.getDate(); // fixed
    function SetDivContent() {
        var div=document.getElementById("date_dependent");
        if (year==2012 && month==10) { // fixed (the JavaScript months order is 0-11, not 1-12)
            if (day>=6 && day<8) { // the following content will be displayed 12/03/2010, 12/04/2010, [...], 12/09/2010, 12/10/2010
                div.innerHTML="images/ud-calendar.png";
            }
            else if (day==9 || day==13) { // this one will be displayed 12/11/2010 and 12/12/2010
                div.innerHTML="images/ud-calendar.png";
            }
            else if (day>13) { // this one - 12/13/2010 and later, until the end of December
                div.innerHTML="images/ud-calendar.png";
            }
        }
        else if (year==2013 && month>=0) div.innerHTML="images/ud-calendar.png"; // OPTIONAL - just to ensure that content 3 is displayed even after December.
    }
    </script>';
    Code (markup):
     
    Rukbat, Nov 25, 2012 IP
  3. Kmonie360

    Kmonie360 Member

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    The reason im asking is this script is not working
     
    Kmonie360, Nov 26, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    What's it doing/not doing? During 2012 it will only work during November. If you run it now, it will execute the last condition, div.innerHTML="images/ud-calendar.png" - but only until the end of November, due to if (year==2012 && month==10). (Actually it doesn't matter what the date is - that's the same image for all dates.)

    And you might want to try

    div.innerHTML="<image src=\'images/ud-calendar.png\' />";
     
    Rukbat, Nov 26, 2012 IP
  5. Kmonie360

    Kmonie360 Member

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    It's only displaying the default, even when the dates are correct. I'm trying to do it with inline php
     
    Kmonie360, Nov 26, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    If by "the default" you mean ud-calendar.png, that's what the code is telling it to display in all cases. (All your if statements end in displaying ud-calendar.png.) I don't know what ud-calendar.png looks like. I assume it's a calendar of some sort. If you expect it to look different for the different if conditions, that's not going to happen. A png is what it is, and it always looks the same.
     
    Rukbat, Nov 27, 2012 IP
  7. Kmonie360

    Kmonie360 Member

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    Sorry I didn't realize the instance I copied and pasted did that. I did that after the script wouldn't display the correct image. So there were 3. "UD-blackwednesday.png". "UD-Christmas.png" and then the calendar. I think I've got one that will work. I will post it later tonight.
     
    Kmonie360, Nov 27, 2012 IP
  8. Kmonie360

    Kmonie360 Member

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8

    I just tested this and it's still not working... maybe it has something to do with the date call?

    <?php
    $date=date("m/d/Y g:ia");
    if(strtotime($date) >= strtotime('11/27/2012 08:30pm') && strtotime($date) < strtotime('11/27/2012 08:40pm')) {
    echo "<a href=\"#\"><img src=\"images/ud-closed-sandy.png\" border=\"0\" ></a>";
    }
    elseif(strtotime($date) >= strtotime('11/27/2012 08:40pm') && strtotime($date) < strtotime('11/27/2012 08:50pm')) {
    echo "<a href=\"#\"><img src=\"images/ud-halloween.png\" border=\"0\" ></a>";
    }
    else {
    echo "<a href=\"#\"><img src=\"images/ud-calendar.png\"  /></a>";
    }
    ?>
    
        <?php
        $date=date("m/d/Y g:ia");
        if(strtotime($date) >= strtotime('11/14/2012 10:00am') && strtotime($date) < strtotime('11/30/2011 1:00am')) {?>
    
    <?php }?>
    PHP:
     
    Kmonie360, Nov 27, 2012 IP
  9. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #9
    That will tell you whether the tests (the 'if' statements) have a chance of working. You can do the same thing before each 'if' statement to see what you should expect the action to be. (You really should be running Firebug and FirePHP, so you can throw variables to the browser to see what your program is doing, step by step if necessary.)

    Also, if you use single quotes for the echo statements, you won't need to escape the double quotes.
     
    Rukbat, Nov 28, 2012 IP