Solved

calculate week of month

Posted on 2013-05-24
14
520 Views
Last Modified: 2013-06-06
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
Comment
Question by:rgb192
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
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

by:Deepak Lakkad
ID: 39196051
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
0
 
LVL 52

Accepted Solution

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

Anyway answer is simple
$week_of_month = ceil($day/7);

Open in new window

Test with
for($d = 1; $d < 32; $d++) {
	$wom = ceil($d/7);
	echo "Day $d: $wom<br/>";
}

Open in new window

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

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
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 52

Expert Comment

by:Julian Hansen
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 108

Expert Comment

by:Ray Paseur
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 52

Expert Comment

by:Julian Hansen
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

Expert Comment

by:Ray Paseur
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";
}

Open in new window

Best to all, ~Ray
0
 

Author Comment

by:rgb192
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 108

Expert Comment

by:Ray Paseur
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 52

Expert Comment

by:Julian Hansen
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 108

Expert Comment

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

Author Closing Comment

by:rgb192
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 52

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 count occurrences of each item in an array.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now