Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# calculate week of month

Posted on 2013-05-24
Medium Priority
528 Views
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
0
Question by:rgb192
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 5
• 2
• +1

LVL 111

Expert Comment

ID: 39195899
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?
0

LVL 11

Expert Comment

ID: 39196051
Hi,

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

0

LVL 59

Accepted Solution

Julian Hansen earned 2000 total points
ID: 39196430
@deepak - not a good link code is confusing

``````\$week_of_month = ceil(\$day/7);
``````
Test with
``````for(\$d = 1; \$d < 32; \$d++) {
\$wom = ceil(\$d/7);
echo "Day \$d: \$wom<br/>";
}
``````
You get
``````Day 1: 1
Day 2: 1
Day 3: 1
Day 4: 1
Day 5: 1
Day 6: 1
Day 7: 1
Day 8: 2
Day 9: 2
Day 10: 2
Day 11: 2
Day 12: 2
Day 13: 2
Day 14: 2
Day 15: 3
Day 16: 3
Day 17: 3
Day 18: 3
Day 19: 3
Day 20: 3
Day 21: 3
Day 22: 4
Day 23: 4
Day 24: 4
Day 25: 4
Day 26: 4
Day 27: 4
Day 28: 4
Day 29: 5
Day 30: 5
Day 31: 5
``````
0

LVL 111

Expert Comment

ID: 39196537
@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.
http://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.
0

LVL 59

Expert Comment

ID: 39196720
@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.
0

LVL 111

Expert Comment

ID: 39196777
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.
0

LVL 59

Expert Comment

ID: 39196804
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.
0

LVL 111

Expert Comment

ID: 39196940
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";
}
``````
Best to all, ~Ray
0

Author Comment

ID: 39203077
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
0

LVL 111

Expert Comment

ID: 39203098
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.
0

LVL 59

Expert Comment

ID: 39203656
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?
0

LVL 111

Expert Comment

ID: 39204249
Please see this link, which seems to be a variant of the question.
0

Author Closing Comment

ID: 39224573
I think this example works for me

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

thanks
0

LVL 59

Expert Comment

ID: 39224760
You are welcome - thanks for the points.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logoâ€¦
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: Tâ€¦
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and â€¦
###### Suggested Courses
Course of the Month4 days, 8 hours left to enroll