Jump to content

Display "Letter Day" on school homepage


jgrubbs

Recommended Posts

Its a 6 letter day cycle:

 

Monday Day A, Tuesday Day B, Wednesday Day C, Thursday Day D, Friday Day E,

Monday Day F, Tuesday Day A, Wednesday Day B, Thursday Day C, Friday Day D,

Monday Day E, Tuesday Day F, Wednesday Day A, Thursday Day B, Friday Day C,

Monday Day D, Tuesday Day E, Wednesday Day F, Thursday Day A, Friday Day B

Link to comment
Share on other sites

You would need a specific date to use as a starting point for A. Using PHP, you can get the Unix timestamps for the current date and the starting date, subtract and divide to figure out how many days are between the two, use the modulus operator to get a number between 0 and 5, and then use the chr function to output the character that corresponds. ASCII character A is 65, so you would add the number to 65 and pass the total to chr to return the letter.

 

You can use mktime to get the timestamp for a specific date, time will return the current timestamp, and the modulus operator is %.

Link to comment
Share on other sites

Javascript has the getTime() function which returns milliseconds since Jan 1, 1970. I guess if you divide this by 86400000 you'll get days since Jan 1, 1970. Then you would have to calibrate it by subtracting some known 'A' day.

var d = new Date();var days = d.getTime()/86400000; // Number of days since midnight Jan 1, 1970

Maybe something like this...

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>ABCDEF Days</title><script>window.onload = init;function init(){var letter = ['A','B','C','D','E','F'];var d = new Date();var day = 24*60*60*1000; //milliseconds per dayvar sept23_2013 = 15971*day;var today = (d.getTime() - sept23_2013)/day; //from Jan 1 1970document.getElementById('date').innerHTML = d.toLocaleString();document.getElementById('dayletter').innerHTML = letter[parseInt(today)%6];}</script></head><body><span id="date"></span><br/>DAY:<span id="dayletter"></span></body></html>

It seems a little strange though. The seconds might be measured from Midnight UTC.

Edited by davej
Link to comment
Share on other sites

Hey Davej,

 

Thanks for the script. I copied it to our site but it didn't work. It looks like it changes some of the code when I paste it and click save.

 

<script>var out;var inp;window.onload = init;function init(){var letter =['A','B','C','D','E','F'];var d =newDate();var day =24*60*60*1000;//milliseconds per dayvar sept24_2013 =15971*day;var today =(d.getTime()- sept24_2013)/day;//from Jan 1 1970document.getElementById('date').innerHTML = d.toLocaleString();document.getElementById('dayletter').innerHTML = letter[parseInt(today)%6];}</script><spanid="date"><br />DAY:<spanid="dayletter"> </spanid="dayletter"></spanid="date">

 

When I view the page, it just says Day:

Link to comment
Share on other sites

Yeah, I would say experiment with it in a separate file until you are sure it works correctly. Then figure out how to add it to your live page without breaking it. I see all sorts of strange things in your pasted version.

 

Here is a version that includes a correction for UTC offset hours. You will need to set this constant to the proper number of hours.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>ABCDEF Days</title><script>window.onload = init;function init(){var letter = ['A','B','C','D','E','F'];var d = new Date();var day = 24*60*60*1000; //milliseconds per dayvar known_A_day = 15972*day;var utc_correction = 5*60*60*1000;//5 hoursvar today = (d.getTime() - known_A_day - utc_correction)/day; //from Jan 1 1970document.getElementById('date').innerHTML = d.toLocaleString();document.getElementById('dayletter').innerHTML = letter[parseInt(today)%6];alert('correct time? '+ (today*24%24).toFixed(3));}</script></head><body><span id="date"></span><br/>DAY:<span id="dayletter"></span></body></html>
Edited by davej
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...