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 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Script to change image at same time every day

    Hello,

    I need a script (must be in JavaScript) that changes an image at the same time every day of the week.

    Let's say it changes at 8am every day.

    I presume it would hold the 7 images in an array and would use something like Date.UTC to ensure everyone was seeing the same image? (Assuming people have their times set right).

    Is this something that's hard to do? I have tried to modify existing scripts but can't get anything to work.

    Any help appreciated.

    Thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,990
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Yes, terribly hard.

    Code:
    <img id = "myimage" src = "">
    
    <script type = "text/javascript">
    
    // change image at 0800 daily according to user's time
    
    var pics = [];
    pics[0] = "Sunday.jpg";
    pics[1] = "Monday.jpg";
    pics[2] = "Tuesday.jpg";
    pics[3] = "Wednesday.jpg";
    pics[4] = "Thursday.jpg";
    pics[5] = "Friday.jpg";
    pics[6] = "Saturday.jpg"
    
    var d = new Date();   // today
    var hr = 8;  // hours to subtract (or add)
    d = d.getTime() - (3600000*hr);  // subtract hr hours
    var nd = new Date(d);
    var dy = nd.getDay();
    document.getElementById("myimage").src = pics[dy];
    
    </script>

    You say you want all users to see the same image, changing at 0800 local time. Obviously that is not the same thing as all users seeing the same image at all times. Or do you want to change the image at 0800 GMT, regardless of the user's local time?


    It wasn't funny but it did make me laugh. - Reporter, Surrey Advertiser.
    Last edited by Philip M; 01-22-2013 at 10:58 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    davidwhite (01-22-2013)

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Yes, terribly hard.

    Code:
    <img id = "myimage" src = "">
    
    <script type = "text/javascript">
    
    // change image at 0800 daily according to user's time
    
    var pics = [];
    pics[0] = "Sunday.jpg";
    pics[1] = "Monday.jpg";
    pics[2] = "Tuesday.jpg";
    pics[3] = "Wednesday.jpg";
    pics[4] = "Thursday.jpg";
    pics[5] = "Friday.jpg";
    pics[6] = "Saturday.jpg"
    
    var d = new Date();   // today
    var hr = 8;  // hours to subtract (or add)
    d = d.getTime() - (3600000*hr);  // subtract hr hours
    var nd = new Date(d);
    var dy = nd.getDay();
    document.getElementById("myimage").src = pics[dy];
    
    </script>

    You say you want all users to see the same image, changing at 0800 local time. Obviously that is not the same thing as all users seeing the same image at all times. Or do you want to change the image at 0800 GMT, regardless of the user's local time?


    It wasn't funny but it did make me laugh. - Reporter, Surrey Advertiser.
    Thanks for the help.

    To be honest, thinking about it, it's not that big a deal that people are in different time zones. So long as it changes once a day for everyone then that's fine.

    Let's just keep it simple and assume it changes 8.00am UTC for everyone, no matter what their local time is.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,990
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by davidwhite View Post
    Let's just keep it simple and assume it changes 8.00am UTC for everyone, no matter what their local time is.
    It would seem to be better to change at 0800 local time for everyone, but if for some reason every user must see the same image (why?) then why change at 0800 rather than midnight UTC?

    <img id = "myimage" src = "">

    <script type = "text/javascript">

    // change image at 0800 UTC daily

    var pics = [];
    pics[0] = "Sunday.jpg";
    pics[1] = "Monday.jpg";
    pics[2] = "Tuesday.jpg";
    pics[3] = "Wednesday.jpg";
    pics[4] = "Thursday.jpg";
    pics[5] = "Friday.jpg";
    pics[6] = "Saturday.jpg"

    var d = new Date(); // today
    var hr = 8; // hours to subtract (or add)
    var off = d.getTimezoneOffset(); // offset in minutes
    off = off *60*1000; // offset in milliseconds
    d = d.getTime() - (3600000*hr) - off; // subtract hr hours and timezone offset
    var nd = new Date(d);
    var dy = nd.getDay();
    document.getElementById("myimage").src = pics[dy];

    </script>

    Note that Javascript relies on the accuracy of the user's computer clock. If it is important that all users always see the same image then you need server-side scripting.
    Last edited by Philip M; 01-22-2013 at 05:09 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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