View Full Version : Get current week number?

11-12-2007, 08:51 PM

I'm trying to make what i thought would be a simple function. but i'm a bit stuck at the moment.

I've found this code on php.net:

$year = date("Y", $date);
$month = date("m", $date);
if( (isset($_GET['year'])) && (intval($_GET['year']) > 1582) ){
$year = intval($_GET['year']);
if( (isset($_GET['month'])) && (intval($_GET['month']) >= 1) && (intval($_GET['month']) <= 12) ){
$month = intval($_GET['month']);
$date = mktime(1, 1, 1, $month, date("d"), $year);
$first_day_of_month = strtotime("-" . (date("d", $date)-1) . " days", $date);
$last_day_of_month = strtotime("+" . (date("t", $first_day_of_month)-1) . " days", $first_day_of_month);
$first_week_no = date("W", $first_day_of_month);
$last_week_no = date("W", $last_day_of_month);
if($last_week_no < $first_week_no) $last_week_no = date("W", strtotime("-1 week",$last_week_no)) + 1;
$weeks_of_month = $last_week_no - $first_week_no + 1;
echo $weeks_of_month;

which will get the number of weeks in the current month.
but I'm having issues figuring out the best way to find the currently week of month. for example, right now it's November week 3.

again seems like this should be simple. but i'm just not seeing the logic yet.


11-12-2007, 09:07 PM
Some considerations ...

partial weeks or full weeks?
does your week start on Sunday or Monday?

you found your script on the internet ...
did you find it here (in the examples)?

11-12-2007, 09:15 PM
Yes, it was in the example on php.net's manual.
also the week start should be Sunday.

and like in my example, these would be partial weeks. since the other way wouldn't make much sense.

"right now it's November week 3"

11-12-2007, 10:11 PM
also the week start should be Sunday.

I never understood how a week can start on Sunday since the weekend is Saturday and Sunday.

11-12-2007, 10:30 PM
Because the week ends are on either side of the week. :D Like bookends.

11-12-2007, 11:36 PM
I smell a conspiracy.

11-13-2007, 01:38 AM
$day = date("m", $date);
$num = $day / 7;
$foo = round($num);

echo "this is week number" . $foo . "in" $not_yet_definded_month_foo

what do you mean by third week in November? if this wasn't a discussion about php I would assume you meant full week (give or take a day or so hence round())

11-13-2007, 05:12 AM
Look at your calendar hanging on your wall ...


The work-week starts on Monday,
but the calendar week starts on Sunday.

I know this argument could go on forever,
that's why I specifically asked him when he
wanted to start his week (not mine).

So, Dec 2, 2007 is the 2nd week of December?

Is that correct?

I know it sounds sort of smart-alec of me to ask
it again, but this type of thing can really cause a
lot of confusion.

To me, Dec 2, 2007 is the 1st week of December.
Not because of the Sunday start thing, but because
it is the starting of the 1st FULL WEEK of December.

So, you need to clarify when week 1 is being determined
for each month. Perhaps if you had an array set-up that
would determine "week 1" for the next 10 years (120 elements
in the array), that would really help you out, and it would
eliminate the problems like Dec. 2, 2007.

11-13-2007, 05:57 AM
The system with week numbers is kinda different in different parts of the world. The ISO system follows the European Norm, which is different from the system used in the USA or the UK, for instance. In the systems used in the UK and the USA a week can be both the last week of one year and the first week of the next year. In the ISO system, a week can only belong to a single year.
Also the calendars differ in what day starts the week, and which days are always guaranteed to be part of the same year. The ISO and UK systems start with a Monday, the system used in the USA starts on a Sunday. The ISO standard, at least four days of the first week week are always part of the first week of the year (or in other terms, the first week whose days belong mostly in the new year). That means that the first Thursday of the year is guaranteed to be in the first week of the year. In the UK system, the first week is always the week containing January 1. Dito in the system used in the USA, the first week is always the week containing January 1. These two systems differ by which day they consider the first of the week, however.

So, it was a relevant question - not only asking which day the week should start with, but also asking what is considered the criteria for a week belonging to a certain year/month etc. Also relevant is asking whether the numbers may overlap. Say one week has three days at the end of one month/year and four days the next month/year. Is that week the last week of the previous month/year or the first week of the next month/year, or maybe it's both?