[PHP3] SV: [PHP3] finding the week no From: johan.gronvall <email protected>
Date: 09/30/98

I made this client javascript a while back ago. If you need it in PHP it
shouldn't be that hard to convert.
To get the week number for a particular date, feed getWeekNumber with a
JS date object.

<script language='JavaScript'>
function getWeekNumber(gwn){
        nrOfWeeksThisYear = getNrOfWeeks(gwn.toLocaleString());
        prevYear = new Date(gwn.toLocaleString());
        prevYear = prevYear.setYear(prevYear.getYear() - 1);
        nrOfWeeksYearBefore = getNrOfWeeks(prevYear);

        e = new Date(gwn.toLocaleString());
        e.setMonth(0);
        e.setDate(1);
        m1 = Date.parse(e.toLocaleString());
        m2 = Date.parse(gwn.toLocaleString());
        days = (m2 - m1) / 3600000 / 24;

        // Find out what day of week corresponds to 1/1
        wrkDate = new Date(gwn.toLocaleString());
        wrkDate.setDate(1);
        wrkDate.setMonth(0);
        day = wrkDate.getDay();

        // In Sweden and according to some ISO rule a week starts on a Monday
        if(day == 0){
                day = 7;
        }

        // Week number one is the week that has the first Thursday in January
        if(day <= 4){
                firstWeekOfYear = 1;
        } else {
                firstWeekOfYear = nrOfWeeksYearBefore;
        }
        day = gwn.getDay();
        if(day == 0){
                day = 7;
        }
        week = Math.ceil((days + 7 - day)/7);
        if(week == 1){
                return firstWeekOfYear;
        } else {
                if(firstWeekOfYear != 1){
                        return week - 1;
                } else {
                        return week;
                }
        }
}
function getNrOfWeeks(gnow){
        // Find out what day of week corresponds to 1/1
        wrkDate = new Date(gnow);
        wrkDate.setDate(1);
        wrkDate.setMonth(0);
        day = wrkDate.getDay();
        // If 1/1 is a Thursday or if 1/1 is a Wednesday and it's leap year,
then it's 53 weeks this year
        if((day == 4) || (day == 3 & isLeapYear == true)){
                return 53;
        } else {
                return 52;
        }
}
function isLeapYear(n){
        y = n.getYear()
        if(y/4 == Math.round(y/4)){
                if(y/100 != Math.round(y/100)){
                        return true;
                } else {
                        if(y/400 == Math.round(y/400)){
                                return true;
                        } else {
                                return false;
                        }
                }
        } else {
                return false;
        }
}
</script>

>----------
>Från: Colin Viebrock[SMTP:cmv <email protected>]
>Skickat: den 29 september 1998 17:38
>Till: Hendrik Johns; PHP3
>Angående: Re: [PHP3] finding the week no
>
>Thus spake Hendrik Johns (at 01:25 AM 9/29/98 +0200) ...
>>>>>
>I have tryed this
>
> $temp= date("z");
> $week= ($temp / 7);
> $week= ceil($week);
>
>but it does not seem to work right.
><<<<
>
>What doesn't work right? This all boils down to deciding where week number
>one starts.
>
>There was some talk about this on the mailing list about 2 months ago. I
>suggest you check the archives.
>
>
>_________________________________________________________________________
>Colin Viebrock Creative Director
>cmv <email protected> Private World Communications
> http://www.privateworld.com
> The other day, I saw a man, a woman and
> two children turn into a corner store.
>
>--
>PHP 3 Mailing List http://www.php.net/
>To unsubscribe send an empty message to php3-unsubscribe <email protected>
>To subscribe to the digest list: php3-digest-subscribe <email protected>
>For help: php3-help <email protected> Archive:
>http://www.php.net/mailsearch.php3
>
>
>

--
PHP 3 Mailing List   http://www.php.net/
To unsubscribe send an empty message to php3-unsubscribe <email protected>
To subscribe to the digest list:  php3-digest-subscribe <email protected>
For help: php3-help <email protected>  Archive:  http://www.php.net/mailsearch.php3