Forum Moderators: open

Message Too Old, No Replies

Page load/redirect based on date/time of year?

redirect, date

         

SleepyW

3:59 am on Jul 17, 2007 (gmt 0)

10+ Year Member



Hi everyone...new here. Nice to find a place for specific web issues. :)

I'm not much of a javascript guy, but I can occasionally edit other code to do what I want. However, I'm stumped on this one.

I'm trying to find code that will load a page based on the date (time of year). In other words, in summer (say Jun 23-Sep 22), I want page1.htm to load, in fall (Sep 23-Nov 22) I want page2.htm to load, etc.

I can't find any code that refers to date ranges (without the year), but have seen some using time of day. Can that type of code be altered to fit these parameters? I'd prefer to have the page be a redirect, but if it's just as easy, I guess I can work it into the menu so the correct page loads right away instead of being redirected.

Anyone have any ideas? Thanks in advance!

Chris

EDIT: I found this code from someone that was trying to have images change based on the season:

if((month<=11)&&(day<21))
season='images/fall.jpg';
if((month<=8)&&(day<21))//before September 21
season = 'images/summer.jpg';
if ((month<=5)&&(day<21))//before June 21
season = 'images/spring.jpg';
if (((month<=2)&&(day<21))¦¦((month==11)&&(day>20)))//before Mar 21 or after December 20
season = 'images/winter.jpg';

How could that be incorporated into determining the season, and then redirecting/loading the proper HTML page based on the results of that function?

[edited by: SleepyW at 4:18 am (utc) on July 17, 2007]

Drag_Racer

4:26 am on Jul 17, 2007 (gmt 0)

10+ Year Member



Yes, you can get the day from javascript, but I would think this would best be done on the server. Either by a mod_rewrite if your on a *nix machine or through a server-side script to redirect or serve content based upon date.

for example this perl snippet would give you the day of the year(0-364)(365 in leap years).

my %day = (localtime)[7];

then you could redirect based upon day of year.

if ( $day && $day < 90 ){ print "Location: this-file.html\n\n"; }
elsif ( $day && $day > 89 && $day < 180){ print "Location: that-file.html\n\n"; }

and so on...

SleepyW

6:28 am on Jul 17, 2007 (gmt 0)

10+ Year Member



Sorry - you've completely lost me. I'm an HTML guy--I don't know anything about perl. Thanks for the reply, but I'll have to find a simpler solution.

I tried to modify some code based on the above, but not sure what/how to tweak it to get it to actually work. I know parts of it are wrong, but left it alone since I don't know what to replace. This was originally set up to display an image on this page based on the season - not redirect/load an HTML page.

<html>
<head>
<script language="JavaScript" type="text/javascript">
function setSeason()
{
if(document.page)
{
var today = new Date();
var month = today.getMonth()+1;
var day = today.getDate();
var season ='page.htm';
if ((month<=11)&&(day<21))
season ='fall.htm';
if ((month<=8)&&(day<21)) //before September 21
season ='summer.htm';
if ((month<=5)&&(day<21)) //before June 21
season ='spring.htm';
if (((month<=2)&&(day<21))¦¦((month==11)&&(day>20))) //before Mar 21 or after December 20
season ='winter.htm';
document.page['season'].src = season;

}

}

</script>
</head>
<body onload="setSeason()">
</body>
</html>

[edited by: SleepyW at 6:29 am (utc) on July 17, 2007]

Drag_Racer

10:46 am on Jul 17, 2007 (gmt 0)

10+ Year Member



your code above is close

function setSeason(){

var today = new Date();
var month = today.getMonth()+1;
var day = today.getDate();
var season ='page.htm';
if (month<=11 && day<21)
season ='fall.htm';
if (month<=8 && day<21) //before September 21
season ='summer.htm';
if (month<=5 && day<21) //before June 21
season ='spring.htm';
if ((month<=2 && day<21)¦¦(month==11 && day>20)) //before Mar 21 or after December 20
season ='winter.htm';
window.location.href = season;
}

this page will give you some stuff on using dates...
[quirksmode.org...]

SleepyW

2:52 pm on Jul 17, 2007 (gmt 0)

10+ Year Member



Getting errors at line 15 and 22 (the season=spring line and body tag).

[edited by: SleepyW at 2:52 pm (utc) on July 17, 2007]

DrDoc

5:15 pm on Jul 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First of all, you need to replace the broken pipes ¦ with "real" vertical pipes.

Second, what if I have the clock on my computer set wrong?

Third, and most importantly -- you do know that spring/summer/fall/winter does not always begin on the 21st, right?

DrDoc

5:17 pm on Jul 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And, something I just noticed ...

What about April 22nd, for example?
Month is <=5 ... but date is not <21 ;)

[edited by: DrDoc at 5:19 pm (utc) on July 17, 2007]

SleepyW

5:29 pm on Jul 17, 2007 (gmt 0)

10+ Year Member



Ahhh...the pipes did it.

I just need it as an approximate - if it's off a day or two, it's close enough. Right now, I have to manually change the initial page load 4 times a year and it's someone else's site. I'm trying to make less work for myself. :)

SleepyW

5:31 pm on Jul 17, 2007 (gmt 0)

10+ Year Member



As for the APril 22 e4xample, doe sit not read the code as month is less than/equal to 5 (May) AND day is less than 21? So 4/22 is less than 5/21.

Thanks again!

SleepyW

5:37 pm on Jul 17, 2007 (gmt 0)

10+ Year Member



DrDoc, you're right -

I altered my computer date to test, and sometimes it works, sometimes not (if the date is the 22nd or beyond, or the month is December, it doesn't work).