?
Solved

Get number of weeks for a given month with PHP

Posted on 2011-02-24
15
Medium Priority
?
3,992 Views
Last Modified: 2012-08-13
Hello all,

Question is simple, however I have difficulty to create a function that would return this value.

September 2012, their is 5 weeks in this month.
February 2011, their is 4 weeks in this month.

How can I find this with a PHP function? Is it possible?

Thank you
0
Comment
Question by:Etdashou
[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
  • Learn & ask questions
  • 7
  • 4
  • 4
15 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34974059
0
 
LVL 5

Expert Comment

by:Ultra_Master
ID: 34974637
The correct code is the following:
<?php

function weeks($month, $year){
        $num_of_days = date("t", mktime(0,0,0,$month,1,$year)); 
        $lastday = date("t", mktime(0, 0, 0, $month, 1, $year)); 
        $no_of_weeks = 0; 
        $count_weeks = 0; 
        while($no_of_weeks < $lastday){ 
            $no_of_weeks += 7; 
            $count_weeks++; 
        } 
	return $count_weeks;
} 

echo weeks(2,2011)."<br/>";
echo weeks(9,2012)."<br/>";

?>

Open in new window

0
 
LVL 1

Author Comment

by:Etdashou
ID: 34980918
hmmm.. Let me test that. I think I haven't ask my question correctly and it is my fault.

If you take a calendar, you see some months have 5 lines of "week". Some have 6, some have 4. This is the number I'm looking for.

But let me test your code.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34981247
So it depends on the day of week that is a month "starter".
0
 
LVL 5

Expert Comment

by:Ultra_Master
ID: 34981918
In that case use the following code:
And, btw, your reasoning was wrong about feb 2011. According to your previous statement you said it has 4 weeks, while it has days distributed over 5 weeks as you can see from the attached picture.
<?php

function weeks($month, $year){
        $firstday = date("w", mktime(0, 0, 0, $month, 1, $year)); 
        $lastday = date("t", mktime(0, 0, 0, $month, 1, $year));
        $count_weeks = 1 + ceil(($lastday-8+$firstday)/7);
        return $count_weeks;
} 
echo weeks(2,2011)."<br/>";
echo weeks(9,2012)."<br/>";

?>

Open in new window

Feb 2011 has days distributed in over 5 "weeks"
0
 
LVL 1

Author Comment

by:Etdashou
ID: 34999478
Ultra Master, you are right, I was wrong with the four weeks.

I tried your code and it seems to work. However, if you look at July 2011, their is 6, however your code output 5. Is it possible because it is Sunday the last day of the month?
0
 
LVL 1

Author Comment

by:Etdashou
ID: 34999866
hmmm
Where I live, the first day of the week is Sunday, not Monday. Is this a problem with your code?
0
 
LVL 5

Expert Comment

by:Ultra_Master
ID: 34999981
July 2011 is 5 not 6. See the attached picture to convince yourself.
But July 2012 is 6. Apparently the counting begins from Sunday and I considered it starting from Monday so use the following updated code and it should work on whatever example.
<?php
function weeks($month, $year){
        $firstday = date("w", mktime(0, 0, 0, $month, 1, $year)); 
        $lastday = date("t", mktime(0, 0, 0, $month, 1, $year)); 
	if ($firstday!=0) $count_weeks = 1 + ceil(($lastday-8+$firstday)/7);
	else $count_weeks = 1 + ceil(($lastday-1)/7);
  return $count_weeks;
} 
echo weeks(2,2011)."<br/>";
echo weeks(7,2011)."<br/>";
echo weeks(7,2012)."<br/>";

?>

Open in new window

July2011.png
0
 
LVL 1

Author Comment

by:Etdashou
ID: 35000118
I attached a picture of my calendar. It is from Quebec - Canada (French).
Weeks start on Sunday (= Di = Dimanche).
Using your new functions I have different results.

echo weeks(2,2011)."<br/>"; 5
echo weeks(7,2011)."<br/>"; 5
echo weeks(7,2012)."<br/>"; 6


I am sorry for this...
cal-sunday.png
cal-sunday-2012.png
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35000651
I'm just curious but I wouldn't be myself without asking this. What is the purpose of this kind of week calculation ? Will it be used only with your local timezone ?
0
 
LVL 1

Author Comment

by:Etdashou
ID: 35000866
I actually create a Calendar in PHP. It is in a Table and I would like to adjust the rowspan for a <td> with the number of weeks that this month will have. It would help me also to use a button that goes from week to week when you press "next" or "previous". If I am on week 5/6, I won't change month, etc.

I didn't realize it would be that much difficult and that I should consider so much conditions, like starting day of a week=Sunday.

I could perhaps add more points to this question.
0
 
LVL 1

Author Comment

by:Etdashou
ID: 35000868
Oh and yes it will be used only in my timezone.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35001238
Could you post your calendar code here ? It would be easier to work with that.
0
 
LVL 5

Accepted Solution

by:
Ultra_Master earned 2000 total points
ID: 35001695
This should solve the problem.  
My initial algorithm was good but adapted to Monday being the first day in the week. My previous update was made in a rush and it seems I neglected a little offset.
This should take care of the situation with Sunday being the first day.
<?php
function weeks($month, $year){
        $firstday = date("w", mktime(0, 0, 0, $month, 1, $year)); 
        $lastday = date("t", mktime(0, 0, 0, $month, 1, $year)); 
	$count_weeks = 1 + ceil(($lastday-7+$firstday)/7);
	return $count_weeks;
} 
echo weeks(2,2011)."<br/>";
echo weeks(7,2011)."<br/>";
echo weeks(7,2012)."<br/>";

Open in new window

0
 
LVL 1

Author Comment

by:Etdashou
ID: 35007234
Ultra Master, you rock!

Thank you.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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

765 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