PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Date and Time » Date » Bug #4130

?

Details

Submitted2005-04-12 20:45 UTC
Fromrachelw at cybrzn dot com
Assignedpajoye
StatusBogus
PackageDate
PHP Version5.0.3
OSLinux
Roadmaps(Not assigned)

Comments

[2005-04-12 20:45 UTC] rachelw at cybrzn dot com

Description:
------------
Was using the getWeeksInMonth() function found in Date.php. I noticed the actual function is implemented in Date/Calc.php (Date_Calc class) in the weeksInMonth() method.

Date package version 1.4.3
Date/Calc.php: version 1.2.6
Date.php: version 1.2.8

configure line:
'./configure' '--prefix=/usr' '--with-config-file-path=/etc' '--with-apxs2' '--with-openssl=/usr/local/ssl' '--with-zlib' '--with-bz2' '--with-cpdflib' '--with-curl' '--enable-dba' '--with-db4' '--with-gd' '--with-jpeg-dir=/usr' '--with-png-dir=/usr' '--with-zlib-dir=/usr' '--with-xpm-dir=/usr' '--with-ttf' '--with-freetype-dir=/usr' '--with-gd-native-ttf' '--with-gettext' '--with-imap=../imap-2004' '--with-mcrypt' '--with-mysql' '--with-ncurses' '--with-unixODBC' '--with-snmp' '--with-openssl-dir=/usr/local/ssl' '--with-imap-ssl=/usr/local/ssl' '--enable-ftp' '--with-pspell'

I am writing a dynamically generated calendar, and for the month of May 2005, it returned 6 for the number of weeks in that month, instead of the correct response of 5. Some other months where it returns 6 weeks when it should return 5 include:

August 2004
January 2006
October 2006
April 2007
July 2007

Upon inspecting any similarities between these months, I noticed that the 1st of the month is on Sunday with all of these. Perhaps that might point you in the right direction.

Reproduce code:
---------------
$year = $_GET["year"];
$month = $_GET["month"];

$first_month = new Date($year."-".$month."-01");
$days_month = $first_month->getDaysInMonth();
$weeks_month = $first_month->getWeeksInMonth();

echo $weeks_month; //expect 5 but it returns 6

Expected result:
----------------
again, for these months (just a sampling):

August 2004
May 2005
January 2006
October 2006
April 2007
July 2007

I expect to get a result of 5 (weeks) vs. 6.

Actual result:
--------------
Actual result is 6 (instead of 5).

[2005-04-13 06:44 UTC] pierre at dotgeek dot org

Sorry, but your problem does not imply a bug in PEAR itself. For a
list of more appropriate places to ask for help using PEAR, please
visit http://pear.php.net/support/ as this bug system is not the
appropriate forum for asking support questions.

Thank you for your interest in PEAR.

It's not the ISO definition of a week that is used here.

That means you will get 6 for 05/2005.

ISO defines the 1st week of a month as the one having a tuesday, using it, you will get 5. This behavior will not change in the Date 1.4 series (BC issues).

--Pierre

[2005-04-13 14:17 UTC] rachelw at cybrzn dot com

So why isn't the ISO definition used here? Isn't that what most people think of when they think of how many weeks are in a month? Furthermore, what exactly IS the definition that is being used? How in the world do you get 6 weeks out of a month like May 2005?

[2005-04-13 14:21 UTC] rachelw at cybrzn dot com

Well in the calendar application I'm writing, I need to know how many weeks in a month, based upon whether or not there is even ONE day in that week because it is used to determine how many rows in an HTML table. I guess I will have to write my own function for that (at least one function isn't too bad considering all the other nice functions I can use from the Date package).

[2005-04-13 19:23 UTC] rachelw at cybrzn dot com

One last thing - the following is the PHP comments for the function weeksInMonth(), found in the Date_Calc class. If this was your intention, then it doesn't work properly, because it does not return the correct number of ROWS (i.e. HTML <tr> tags) for outputting a calendar. Any month which begins on a Sunday, and which has 5 weeks (rows), returns 6 instead.

/**
* Returns the number of rows on a calendar month
*
* Useful for determining the number of rows when displaying a typical month calendar.

function weeksInMonth($month = 0, $year = 0)
...

[2005-04-13 19:28 UTC] pierre at dotgeek dot org

Forget to mention that there will be a getIsoWeeksInMonth() or something similar :). No date yet, but move to verified.

--Pierre

[2005-04-13 22:02 UTC] rachelw at cybrzn dot com

It's a quick fix, and perhaps a bit crude, but it worked for me. Hopefully it will help anyone else in a similar situation (please let me know if I should also post this somewhere else):

require_once("Date/Calc.php");

$days_of_week = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$num_days_week = 7;

function weeksInMonth ($month,$year) {
//get the numerical value (0-6) of the first day of the month
$firstDay = Date_Calc::dayOfWeek(01,$month,$year);

//now subtract that value with the # of days in a week, to get the # of days in the first week
$numDaysInFirstWeek = $num_days_week - $firstDay;

//get the # of days in this month
$daysInMonth = Date_Calc::daysInMonth($month,$year);

//get the numerical value (0-6) of the last day of the month
$lastDay = Date_Calc::dayOfWeek($daysInMonth,$month,$year);

//add 1 to that value to get the # of days in the last week
$numDaysInLastWeek = $lastDay+1;

//subtract the # of days in first and last weeks with the # of days in month to get remaining days
$numDaysInOtherWeeks = $daysInMonth - $numDaysInFirstWeek - $numDaysInLastWeek;

//divide the remaining days by # of days in a week, and add the first & last week to get total weeks
return ($numDaysInOtherWeeks / $num_days_week) + 2;
}