Switch image by time\date??

Discussion in 'JavaScript' started by kreven, Jun 28, 2007.

  1. #1
    Hi,

    I am in a pinch with this situation. A client wants to change a few images on his website at a rediculous time when we are all sleeping and I need a script that can automatically do this for me. I will already have the images in the image folder but just want to switch them out automatically by giving a time and date ( i assume from that servers particular computers date/time).

    Is this possible? Can anyone help me?

    Thanks so much!

    Kreven
     
    kreven, Jun 28, 2007 IP
  2. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #2
    You can do this with

    setTimeout(function_name,INTERVAL) // INTERVAL is in miliseconds

    in function_name do your action when given time is occurred.
     
    gibex, Jun 28, 2007 IP
  3. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #3
    If you need different images on your pages depending on SERVER date/time simply return your HTMLs with the proper images; this is called dinamically generated htmls.

    If you can't do this because your HTML pages are static, you can use a javascript function to choose the image depending on SERVER date/time and not CLIENT date/time. IMO the best way is using getUTCDate javascript function.
    Take a look at this page for info about javascript object date:
     
    ajsa52, Jun 28, 2007 IP
  4. kreven

    kreven Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Figured it out!

    Here are the results:

    javascript code:

    	
    
    //MAIN IMAGE 
    function changeImg01(imgNew,imgOld) {
    	var ck=timeTest(); // calls to check time, returns true/false
    	var mImg; // put the specific image into this variable
    	
    	if(ck==true) { mImg=imgNew; } else { mImg=imgOld; }
    	document.write('<img src="' + mImg + '"  width="390" height="381">'); // here you should edit the width and height
    }	
    
    function timeTest() {
    
    month = 'June';      // change the month to the one you want in the same format as shown
     date = '29';        // change the date to the one you want in the same format as shown
     year = '2007';      // change the year to the one you want in the same format as shown
    
     theDate = month + ' ' + date + ' ' + year;
    
       now = new Date();
       setdate = new Date(theDate);
    
       timer = (setdate - now) / 1000 / 60 / 60 / 24;
       timer = Math.round(timer);
    
     if (timer >= "1") return false;	// if before the date use original image
     if (timer <= "0") return true;	// if on or after the date change image
    
    }
    
    
    Code (markup):
    put in HTML where you want the image to be replaced (first parameter is the new img, the second is the original one):

    
    <script language="javascript" type="text/javascript">
         changeImg01("topimg/illust_sum.jpg","topimg/illust_spr.jpg");
    </script>
    
    Code (markup):
    Repeat the function "changeImg01" to be "changeImg02", and rinse.

    Hope this helps other people that want to do this. Its not perfect but it gets the job done.

    Cheers,
    Kreven

    P.s. This is the clients server so they will be seeing the same time. For this situation, it changes on a certain date, meaning instantly after midnight. Which is what my client requested.
     
    kreven, Jun 28, 2007 IP
  5. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #5
    after midnight is not simultaneously worldwide. When is 12:00 on your city it's 03:00 on other country and 18:00 on another. So you should use UTC times instead of local times.
     
    ajsa52, Jun 28, 2007 IP