Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

calculate week of month

given the date is there a php function to find week of month (not week of year)
week of month
todays date may 24th is the 4th week of may
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

How you define week is fairly simple. It is a seven day period beginning on a Sunday.  Therefore the first "week" of this month was not a complete week.  If you want to define week as any number of days, so long as it starts the month, then the definition changes.  For example, in June (next month) June 1st happens on a Saturday, so the first Sunday in June is June 2.  Would you still want the singleton June 1 to count as a whole week?

What is your definition of week?
Avatar of Deepak Lakkad
Hi,

Here is a link which will be helpful to you

http://i-code-today.blogspot.in/2009/03/calculating-week-of-month-from-given.html

- Deepak Lakkad
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@julianH: Just for grins, try it again with for($d = 1; $d < 365; $d++) {
and you will see that the week number needs some sanity checks.

Practical Applications #4 and #6 in this article would seem to be applicable.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Week number has an ISO-8601 standard definition that is relative to year, but not to month.  The term "month" is ambiguous since different months have different numbers of days.  When you wrap the term "week" with the term "month" you risk introducing this ambiguity into an otherwise standard definition.  Hence my question to clarify how the author wants to define the week number, relative to the month.

PHP strtotime() and date() work well together to get an answer.  All we need, to provide proven code, is the clarification of the author's intent.
@Ray - why would I want to do a loop to 365 days

I am going on the assumption that the day is the day of the month.
why would I want to do a loop to 365 days
Good question!  It would show the effect that would arise if incorrect information was accidentally input to the algorithm.  I think the week number of the month is not a standardized thing, so any of our answers might be OK for the author of the question.  We can't know because the question is incomplete in its description of the problem.

In my experience, it's almost always better practice to use the PHP built-in functions for DATETIME calculations.  I say that mostly because I am lazy and already know how to do most of what I need with strtotime() and date() or the related OOP built-ins.
rgb192 often leaves us guessing as to his full requirements however in this case I think he was fairly specific.

He specifically excluded week of year and gave an example of 24th of the month being week 4.

Based on that I answered the question.
Yes, I understand, and I think your answer makes sense.  I've been trying to get rgb192 to take some interest in the DATETIME article.

Looking forward to June (which is almost here) the first week has only one day on a conventional western calendar because June starts on Saturday.  Do you count that one day as a week?  Or do you count the first seven days as the first week, no matter which weekday begins the month?  The ISO standard about the week number of the year has some interesting anomalies, to wit, you can have week number 53 in some years because the standard starts all weeks consistently, on Monday.  See date('W') for the explanation.  It's not clear to me whether rgb192 wants to comply with the standard for when weeks start.  USA calendars do not, but that's a separate matter.

Here is the demo about the week number when applied to a year.  It may or may not make any difference to this question but it's interesting to see how PHP handles some of these things.
http://www.laprbass.com/RAY_temp_julianh.php
<?php // RAY_temp_julianh.php
error_reporting(E_ALL);
echo '<pre>';

$y = 2000;
while ($y < 2014)
{
    $y++;
    $w = date('W', strtotime("01-01-$y"));
    echo PHP_EOL . "JANUARY 1 of YEAR $y IS IN WEEK $w";
}

Open in new window

Best to all, ~Ray
Avatar of rgb192

ASKER

by: julianHPosted on 2013-05-25 at 07:01:28ID: 39196430

Day 28: 4
Day 29: 5
Day 30: 5
Day 31: 5

how can I tell input month

because some months have 28 days-31 days

or does that not matter
Well, the only month that will have exactly 28 days will be February, and this will be true 3/4 of the time.  The other months will have more than 28 days.  You may choose to define the last few 28+ days as "week 5."  

how can I tell input month
You can use the timestamp of any day in the month and find date('m') or similar.
how can I tell input month

I don't understand the question - the only month you can tell is February - the rest is ambiguous but still don't know what you mean by how can you tell what the input month is - surely you will provide this?
Please see this link, which seems to be a variant of the question.
Avatar of rgb192

ASKER

I think this example works for me

I do not know (or understand) of an instance where it would not work

thanks
You are welcome - thanks for the points.