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.
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.
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...
Ho$H
Thats what I was trying to do, but the maths involved have bested me. I can't get a reliable result.
mail me what you have done so far so that I can have a look see if I can help in any way.
Ho$H
and if you want the the week in the currentmonth devide result by month eg 5 here. Hope that helps youPHP Code:function getWeekNoByDay($year = 2007,$month = 5,$day = 5) {
return ceil(($day + date("w",mktime(0,0,0,$month,1,$year)))/7);
}
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
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
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.
Nope there can be 5 weeks in a month too
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...
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.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;
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 by djRobbieF; 05-03-2007 at 05:00 PM.
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 by Ratsalad; 23-03-2007 at 11:47 AM.
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.
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.Code:$weekNum = date("W") - date("W",strtotime(date("Y-m-01"))) + 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;
}
Bookmarks