What’s the number of the week in a month ?
I just finished most of the work on the recurrence of events. What is special programming, whatever the language is that where you thought you quickly put a time of madness to carry out certain tasks.
This was the case with the calculation of the number of weeks in the month!
A week to reach a satisfactory result. As I arrived alone and without help, I told myself that this might help someone go through there.
For a given date, what week in the month is a part ? Nothing is easier to calculate in appearance. Only appearance. The order of date is digital, the week is based on names of days, Sunday, Monday, Tuesday etc. .. A week may begin at any number of days. You touch to the difficulty ….
PHP provides us with sophisticated functions. For example the function date().
By consulting the documentation of the function date(), you can found this:
- Parameter: “W”
- Description: W-8601 ISO week number of year, weeks starting on Monday
Interesting, but it is the number of weeks in the year. A condition to satisfy the requirement that the week beginning Monday. So this is not what we need.
Looking on the Internet.
We often find proposals for solutions like this here:
function week_number( $date = 'today' ) { return ceil( date( 'j', strtotime( $date ) ) / 7 ); }
The author claims that its tests are positive. I let you check a few dates and see all the absurdity of the proposal. Is based on the number of days to determine the number of weeks ? Provided that every week beginning the same day. And that day is the same throughout the world. This is not the case. And users of Wordpress know it well. That is a lot of unacceptable conditions.
But at least the idea is there and it can represent a starting point.
So what?
So he’ll have to think and fend for themselves.
Needless to make tons, I give you the result.
So initially we have a English date format: $givendate
(ie. 2008-05-10)
Here is the calculation of the necessary data:
- Decomposition of the date by year, month, day:
- Number of the first day of the week:
- The last day of the month of the date:
(it’s also the number of days in the month) - The weekday number of the first day in the month:
- Test the june 2008 and you will understand why the exception it’s necessary :
list($year, $month, $day) = explode("-", $givendate);
// We choose here 0 for sunday. // This value must be modified during tests of course $first_day = '0'; // But in Wordpress there is a option dedicaced : $first_day = get_option('start_of_week');
$lastday = date( 't', strtotime( $givendate));
$wdfirstday = date("w", mktime(0, 0, 0, $month, 1, $year));
$except = ((1 + $wdfirstday - $first_day) == 0) ? 1 : 0 ;
We finally get this:
$weeknum = ceil((($day + $wdfirstday - $first_day) / 7) + $except);
My tests were carried out by self control results in three successive years.
I can send the test code file to those wishes ![]()











bargin cheap insurence motor for hire www americannationalpropertycasualty provo ut astra…
raged cure!green tuned….