Solved

calculate week of month

Posted on 2013-05-24
14
514 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
Comment Utility
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
Comment Utility
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 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
@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
Comment Utility
@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 51

Expert Comment

by:Julian Hansen
Comment Utility
@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
Comment Utility
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 51

Expert Comment

by:Julian Hansen
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Expert Comment

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

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
Please see this link, which seems to be a variant of the question.
0
 

Author Closing Comment

by:rgb192
Comment Utility
I think this example works for me

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

thanks
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are welcome - thanks for the points.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mysqli Query 5 40
Button Click 11 23
Paging Using PHP 7 31
Strip leading 0 from a var 3 6
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 …

771 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

10 Experts available now in Live!

Get 1:1 Help Now