[ back to toc ]

Week Number

Date: 2002/01/04 03:08

Q:
Hi,
do you know how to get the current week number?
There isn't some kind of function to calculate it.
I tried several things using localtime but noting really worked.

Thanks,

*NAME-DELETED*
A:
No, actually I do not know. Last year I used the calculation:

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime( time() );

$year += 1900;
$mon ++;
# this works only because this year January 1, 2001. was monday.
$week = (($yday-1) / 7)+1;
$week =~ s/\..*$//;
$mon = '0' . $mon if $mon < 10;
$mday = '0' . $mday if $mday < 10;
$day = "$year$mon$mday";

Today I changed one line to

$week = ($yday / 7)+1;

This was a quick and dirty solution. I should have used something like

$week = (($yday-$yfd) / 7)+1;

where $yfd (year first day) is calculated based on the first day of the
week, being

Monday 1
Tuesday 2
Wednesday 3

and so on.

However counting the weeks is not well defined. There are different
definitions on which week is the first in the year.

Regards,
Peter

[ back to toc ]