PDA

View Full Version : Every week counter help!



jeremyp
Nov 23rd, 2009, 03:42 PM
I need help with a counter that will either display a different image everyweek or change text every week. The scenario is a counter will be on the top right corner of the page, and every week the number will change from 1 to 2 to 3... to 52...

I searched on this forum for answers, i found some similar for a different day counter. Any help would be great!

Philip M
Nov 23rd, 2009, 04:15 PM
Try this:-


<div id = "myweek"></div>

<script type = "text/javascript">
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}

var today = new Date();
var weekno = today.getWeek();
document.getElementById("myweek").innerHTML = "Week number: " + weekno;
</script>


Quizmaster: In Greek mythology, can you name one of the five rivers of Hades, the underworld?
Contestant: The Thames.

This is based on the week in which January 1st falls as being Week 1, though not all of the days in that week are necessarily in the current year. A week is defined as running from Sunday - Saturday. The International Standards Organisation (ISO) have a different definition for week numbering, not necessarily starting on 1st January, and all weeks have exactly seven days, but I assume that is not needed here.

jeremyp
Nov 23rd, 2009, 04:43 PM
question: what if January 1st wasn't the start date?

Philip M
Nov 23rd, 2009, 08:01 PM
question: what if January 1st wasn't the start date?

Meaning? And why does it matter?


var please = false;
var thanks = false;
if (!please && !thanks) {
var interestLost = true;
var moreHelp = 0;
}

jeremyp
Nov 23rd, 2009, 09:26 PM
Meaning? And why does it matter?


var please = false;
var thanks = false;
if (!please && !thanks) {
var interestLost = true;
var moreHelp = 0;
}


haha, I said thanks originally then i re-did my post because I had that question. But sorry, thanks for all your help it is def appreciated.

The reason it matters is the counter is for 52 weeks, but it isn't based on the calendar weeks. So, whenever they choose to start the counter it has to ascend from 1 to 52 no matter what the calendar week is.

again, thanks for your help!!

jmrker
Nov 23rd, 2009, 11:11 PM
Assuming you save the initial count in a cookie (and the user doesn't clear cookies occasionally)
or you save the count somewhere on the server, your count doesn't matter what the calendar week.

Example:
origCount = 30; // assumed saved previously in cookie or on server from calendar week
var today = new Date();
var weekno = today.getWeek(); // assumed later in year and found to be 40
var currCount = weekno - origCount;
if (currCount < 0) { currCount += 52; } // example result = 10

Different example:
origCount = 30; // assumed saved previously in cookie or on server from calendar week
var today = new Date();
var weekno = today.getWeek(); // assumed later in NEXT year and found to be 20
var currCount = weekno - origCount; // example result = -10
if (currCount < 0) { currCount += 52; } // example result = 42

Either way, you have your count. :thumbsup:

Philip M
Nov 24th, 2009, 08:47 AM
The reason it matters is the counter is for 52 weeks, but it isn't based on the calendar weeks. So, whenever they choose to start the counter it has to ascend from 1 to 52 no matter what the calendar week is.



Clear as mud. Are you saying that for each visitor to your site the counter begins at Week 1 regardless of the actual date, and then increments once per week for 52 weeks? If so you really need server-side scripting as cookies can (and often are) deleted.