PDA

View Full Version : change iframe at different time of day



OPF Radio
12-15-2012, 07:29 PM
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.


<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>

traq
12-15-2012, 08:47 PM
... 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?

OPF Radio
12-15-2012, 10:40 PM
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????



<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>

OPF Radio
12-16-2012, 01:21 AM
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!


<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>

traq
12-16-2012, 01:34 AM
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:

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 /* code goes here */ ?>
for html...............
<!-- markup goes here -->.....
for js/css/other.......
code goes here................
results in:
<?php /* code goes here */ ?>

<!-- markup goes here -->

code goes here

jscheuer1
12-16-2012, 03:42 AM
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:


hr = day.getHours()

do:


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.

traq
12-16-2012, 06:21 AM
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:

window.location= (location.href="http://www.opfradio.co.uk/requests.php");

<?php
# /requests.php

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

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

print $content;
exit;