Calculating the week in the month (PHP)

Nod

Executive Member
Joined
Jul 22, 2005
Messages
9,475
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

Active Member
Joined
Dec 14, 2005
Messages
44
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

Executive Member
Joined
Jul 22, 2005
Messages
9,475
Thats what I was trying to do, but the maths involved have bested me. I can't get a reliable result.
 

wjc

Active Member
Joined
Dec 14, 2005
Messages
44
mail me what you have done so far so that I can have a look see if I can help in any way.
 

Kloon

Expert Member
Joined
Nov 6, 2006
Messages
1,665
PHP:
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

Executive Member
Joined
Jul 22, 2005
Messages
9,475
PHP:
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

Expert Member
Joined
Nov 6, 2006
Messages
1,665
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

Expert Member
Joined
Nov 6, 2006
Messages
1,665
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

Executive Member
Joined
Jul 22, 2005
Messages
9,475
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.
 

djRobbieF

New Member
Joined
Mar 5, 2007
Messages
1
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:
// 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
 
Last edited:

Ratsalad

Active Member
Joined
Feb 27, 2007
Messages
45
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.
 
Last edited:

BobbyMac

Expert Member
Joined
Jan 17, 2007
Messages
3,353
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.
Code:
$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

New Member
Joined
Mar 14, 2013
Messages
1
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;
}
 
Top