Results 1 to 7 of 7

Thread: change iframe at different time of day

  1. #1
    Join Date
    Dec 2012
    Location
    Manchester
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default change iframe at different time of day

    Daily iframe content changes the iframe each day of the month.

    Could this be adapted to change the iframe at a set TIME of day (24hour clock) and then change it back later in the day?

    Example:

    iframe1.html at 1am
    iframe2.html at 2am
    iframe1.html at 10am

    if time<=10am then iframe1.html
    else
    if time >=10am then iframe2.html

    something like this......
    EXAMPLE: I am not a script writer but it will give you some idea as to what I need.
    HTML Code:
    <script language="JavaScript">
    
    	day = new Date()
    	hr = day.getHours()
    
            
    
    	if  ((hr == 2) || (hr == 3) || (hr == 4))
    	document.load("http://www.opfradio.com/requests000busy.html")
    
    	if  ((hr == 5) || (hr == 6) || (hr == 7))
    	document.load("http://www.opfradio.com/requests000busy.html")
    
            if  ((hr == 8) || (hr == 9))
    	document.load("http://www.opfradio.com/requests000busy.html")
    
    	
    if  ((hr == 10) || (hr == 11))
    	document.load("http://www.opfradio.com/requests000.html")
    
    
    if  ((hr == 00) || (hr == 00))
    	document.load("http://www.opfradio.com/requests000.html")
    
    
    	document.load("http://www.opfradio.com/requests000.html")
    </script>
    Last edited by traq; 12-15-2012 at 08:45 PM. Reason: please use the forum's BBCode tags when posting code.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    ... 10 am according to whose clock?

    The approach you're hinting at there will use the visitor's clock, so (for example) users in the U.S. and the U.K. will see different content, even though they're visiting at the same time. Is that what you want?

  3. #3
    Join Date
    Dec 2012
    Location
    Manchester
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    sorry - my fault - Everyone in the world would need to be at LONDON GMT for everyone (serverside)

    I have tested this out and it seems to work...... very impressed with myself.... but it uses the users clock????


    Code:
    <body bgcolor="#000000" text="#ededed">
    <script language="JavaScript">
    
    	day = new Date()
    	hr = day.getHours()
    
            
    
    	if  ((hr == 2) || (hr == 3) || (hr == 4))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000busy.html")
    
    	if  ((hr == 5) || (hr == 6) || (hr == 7))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000f.html")
    
            if  ((hr == 8) || (hr == 9))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000f.html")
    
            if  ((hr == 10) || (hr == 11))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
    	
            if  ((hr == 12) || (hr == 13))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
            if  ((hr == 14) || (hr == 15))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
            if  ((hr == 16) || (hr == 17))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
            if  ((hr == 18) || (hr == 19))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
            if  ((hr == 20) || (hr == 21))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
            if  ((hr == 22) || (hr == 23))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000f.html")
    
            if  ((hr == 00) || (hr == 24))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
    
            if  ((hr == 00) || (hr == 1))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
    	
    </script>
    Last edited by keyboard; 12-16-2012 at 02:29 AM. Reason: Format: Code Tags [code][/code]

  4. #4
    Join Date
    Dec 2012
    Location
    Manchester
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Just need to make sure that it is always GMT for everyone (like you mentioned above) never thought of that

    But I am impressed with my little script and it does work too!
    Code:
    <script language="JavaScript">
    
    	day = new Date()
    	hr = day.getHours()
    
            
    
    	if  ((hr == 2) || (hr == 3) || (hr == 4))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000f.html")
    
    	if  ((hr == 5) || (hr == 6) || (hr == 7))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000f.html")
    
            if  ((hr == 8) || (hr == 9))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000f.html")
    
            if  ((hr == 10) || (hr == 11))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
            if  ((hr == 12) || (hr == 13))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
            if  ((hr == 14) || (hr == 15))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
            if  ((hr == 16) || (hr == 17))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
            if  ((hr == 18) || (hr == 19))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
            if  ((hr == 20) || (hr == 21))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
            if  ((hr == 22) || (hr == 23))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
            if  ((hr == 00) || (hr == 1))
    	window.location= (location.href="http://www.opfradio.co.uk/requests000.html")
    
    
    	
    </script>
    Last edited by keyboard; 12-16-2012 at 02:28 AM. Reason: Format: Code Tags [code][/code]

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    yeah - javascript runs on the user's computer, so it gets the user's time.

    If you want it to be set according to "your" time, you'd be better off setting it from your server. Do you have PHP or another server-side language available?

    BTW, you don't need to check each hour individually. Use comparison operators to create ranges based on when you want things to change:
    Code:
    if( hr >= 2 && hr <= 4 ){ /* after 2am, before 5am */ }
    else if( hr >= 5 && hr <= 7 ){ /* after 5am, before 8am */ }
    else if( /* etc. */ ){ /* etc. */ }
    Also,
    Please use the forum's bbcode tags and indent your code to make it more readable:

    for php code............[php] <?php /* code goes here */ ?> [/php]
    for html...............[html] <!-- markup goes here -->.....[/html]
    for js/css/other.......[code] code goes here................[/code]

    results in:
    PHP Code:
     <?php /* code goes here */ ?>
    HTML Code:
     <!-- markup goes here -->
    Code:
     code goes here

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Assuming that the server is correct, it's the best bet for getting the correct time for all. Even if it's not GMT, you can ask it for GMT or determine what zone it's in and make the correction 'manually' in your PHP or javascript code.

    But if you don't want to involve the server, you can also ask the user's computer for GMT. In stead of:

    Code:
    hr = day.getHours()
    do:

    Code:
    hr = day.getUTCHours()
    This is still less reliable than using a server that you know to be accurate because, although the user's computer will give you it's version of UTC, if the user's clock is off, the time will still be wrong. But for most cases it's fine. It really depends upon how accurate you want to be.

    However, you should also consider that most user's computers are accurate, and it's always possible that your server might not be, at least not at times.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I think this would come down to a matter of importance: does it really matter if the time is correct? is it critical that any two users see the same content at the same time of day? If so, you should handle it from the server. Aside from the possibility of javascript getting the wrong time, it's also under the complete control of the user.

    A possible example:
    Code:
    window.location= (location.href="http://www.opfradio.co.uk/requests.php");
    PHP Code:
    <?php
    # /requests.php

    // new datetime object using current UTC time
    $datetime = new DateTimetime() );
    // get the hour `G` (24-hr format with no leading 0)
    $hr $datetime->format"G" );

    // your tests here
    if( $hr ){ 
        
    $content = include( '/before/2am/page.html' ); 
    }elseif( 
    $hr ){ 
        
    $content = include( '/between/2am/and/5am/page.html' ); 
    }elseif( 
    /* etc. */ ){
        
    /* etc. */ 
    }

    print 
    $content;
    exit;

Similar Threads

  1. Day and time change.js
    By andysim in forum JavaScript
    Replies: 5
    Last Post: 02-20-2012, 12:34 PM
  2. Change Display Time Format
    By newphpcoder in forum PHP
    Replies: 1
    Last Post: 10-26-2011, 01:50 PM
  3. change picture after given time
    By mukkii in forum PHP
    Replies: 10
    Last Post: 06-28-2010, 07:33 PM
  4. Want to change position of time and date
    By scott452 in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 07-06-2006, 08:22 AM
  5. Want to change position of time and date
    By scott452 in forum HTML
    Replies: 0
    Last Post: 07-06-2006, 08:22 AM

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
  •