jgrubbs Posted September 20, 2013 Share Posted September 20, 2013 Our school uses a 6 "letter day" cycle (A-F). I would like to be able to get some code to cycle through the letters daily. Monday is Day A, Tuesday is Day B, etc. Is there an easy way to do this? Here is the page for context: http://www.pgsd.org/hs Thanks for any help! Link to comment Share on other sites More sharing options...
Ingolme Posted September 20, 2013 Share Posted September 20, 2013 You can use Javascript or PHP to associate days of the week to letters: http://w3schools.com/jsref/jsref_getday.asp http://w3schools.com/php/func_date_idate.asp Link to comment Share on other sites More sharing options...
jgrubbs Posted September 20, 2013 Author Share Posted September 20, 2013 Hey Ingolme, I checked these out early, but wasn't sure they would work. Since we are on a 6 day schedule, I can't associate a number to a day. Is there a way to display a different letter each day and have it go back to A after F? Link to comment Share on other sites More sharing options...
Ingolme Posted September 20, 2013 Share Posted September 20, 2013 So if A is Monday, next A would be next Sunday, then the following A would be on a Saturday? Link to comment Share on other sites More sharing options...
davej Posted September 20, 2013 Share Posted September 20, 2013 Our school uses a 6 "letter day" cycle (A-F). Fine, give us a one month example so we know what you mean. Link to comment Share on other sites More sharing options...
jgrubbs Posted September 23, 2013 Author Share Posted September 23, 2013 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 More sharing options...
justsomeguy Posted September 23, 2013 Share Posted September 23, 2013 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 More sharing options...
davej Posted September 23, 2013 Share Posted September 23, 2013 (edited) 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 September 24, 2013 by davej Link to comment Share on other sites More sharing options...
jgrubbs Posted September 24, 2013 Author Share Posted September 24, 2013 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 More sharing options...
Ingolme Posted September 24, 2013 Share Posted September 24, 2013 By putting the whole script on one line the comments have removed a lot of the code. Link to comment Share on other sites More sharing options...
davej Posted September 24, 2013 Share Posted September 24, 2013 (edited) 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 September 24, 2013 by davej Link to comment Share on other sites More sharing options...
justsomeguy Posted September 24, 2013 Share Posted September 24, 2013 Keep in mind also that doing that in Javascript is going to use whatever the date and time settings are for the user's computer, not a constant date that everyone sees. Link to comment Share on other sites More sharing options...
davej Posted September 24, 2013 Share Posted September 24, 2013 Good point. Yeah, if there is already server-side posting the date then it should also do this. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now