PDA

View Full Version : Calculating the week in the month (PHP)

Nod
12-02-2007, 08:28 AM
So there is a function for the day of the week (date('d')), the total days in a month (date('t')) and the week in the year, but how can I get the week number (1-4/5) in the month?

Any help would be appreciated.

wjc
12-02-2007, 09:20 AM
Why don't you write your own function. You must put on your thinking cap though and fail a couple of dozen times depending on your experience.

Happy coding...

Nod
12-02-2007, 11:03 AM
Thats what I was trying to do, but the maths involved have bested me. I can't get a reliable result.

wjc
12-02-2007, 12:05 PM
mail (theenlone@24.com) me what you have done so far so that I can have a look see if I can help in any way.

Kloon
12-02-2007, 01:49 PM
function getWeekNoByDay(\$year = 2007,\$month = 5,\$day = 5) {
return ceil((\$day + date("w",mktime(0,0,0,\$month,1,\$year)))/7);
}

and if you want the the week in the currentmonth devide result by month eg 5 here. Hope that helps you

Nod
12-02-2007, 02:07 PM
function getWeekNoByDay(\$year = 2007,\$month = 5,\$day = 5) {
return ceil((\$day + date("w",mktime(0,0,0,\$month,1,\$year)))/7);
}

and if you want the the week in the currentmonth devide result by month eg 5 here. Hope that helps you

Does not work for me. If I give it the current year, month and the 15th, it gives me the week of the month as 2. Did the divide by month as well. Shouldn't the result be 3?

Kloon
12-02-2007, 02:34 PM
2 would be correct, it is only the 12th. This means your in the second week of feb, or so i think, if you think it should be 3 try rounding the value up

Kloon
12-02-2007, 02:39 PM
Sorry mate didnt check the code properly, no need to devide by month it already gives you the correct answer. If i run it with todays date i get 3

Nod
12-02-2007, 03:47 PM
I'll have to speak to the guy that wrote the spec, because it seems that he only caters for 4 weeks in a month, which there aren't.

Thanks for all the help. It is appreciated.

Kloon
12-02-2007, 03:59 PM
Nope there can be 5 weeks in a month too

Nod
12-02-2007, 06:42 PM
Nope there can be 5 weeks in a month too

I'm aware of that, yes.

djRobbieF
05-03-2007, 04:50 PM
Why over-complicate things? I googled this very thing (it's how I found your question) and it seems anyone who has "solved" this is generating like 2000 lines of code to do something so simple.

Here's my idea on how this should be done...

// Code by RobbieF.com - determines current week of the month
// March 5, 2007
\$i=0;
\$week=0;
if (date("N", mktime(0, 0, 0, date(n), 1, date(Y))) <= "6") \$week++;
while (\$i <= date(j)) {
if (date("N", mktime(0, 0, 0, date(n), \$i, date(Y))) == "7") \$week++;
\$i++;
}

echo "Current week of the month: " . \$week;

All that does is simply count how many Sundays have passed (the "7") since the beginning of the month, and also checks if the first day of the month falls on or before Saturday (which would cause it to be week 1... this solves the "week 5" thing). That makes sense to me to do what you're wanting. I dunno why we'd wanna over-complicate something like that.

I haven't really tested this, and it's just a quick mockup - but it should at least give you a good starting point... but it should work.

Hope it helps!!
www.RobbieF.com (http://www.robbief.com)

23-03-2007, 10:28 AM
Actually there's an easier way.
The date function can return week number from a timestamp. Use mktime() to format your date as a UNIX timestamp.
eg:
date ("W",mktime(0, 0, 0, 3, 22, 2007));
where 3 is month, 22 is day and 2007 is year.
Hope this helps.

BobbyMac
23-03-2007, 01:09 PM
This line of code will return the week number. Note: PHP's "W" parameter in the Date function assumes that Monday is the first day of the week, so if you need to work from Sunday as the first day of the week, you'd need to build that logic in yourself.

\$weekNum = date("W") - date("W",strtotime(date("Y-m-01"))) + 1;

We add the 1 at the end because the "W" parameter returns a zero based result, so if you leave off the + 1, then you must remember the first week will be returned as 0.

klihini
14-03-2013, 10:55 PM
function getWeekOfTheMonth(\$YY,\$MM,\$DD){
\$weekNum = date("W",mktime(0,0,0,\$MM,\$DD,\$YY)) - date("W",mktime(0,0,0,\$MM,01,\$YY)) + 1;
return \$weekNum;
}