PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Date and Time » Calendar » Bug #8758

Week-class displays wrong year

Details

Submitted2006-09-21 13:35 UTC
Frommacristo at gmx dot net
Assignedquipo
StatusClosed
PackageCalendar
PHP Version5.1.6
OSDebian GNU/Linux Etch
Roadmaps(Not assigned)

Comments

[2006-09-21 13:35 UTC] macristo at gmx dot net

Description:
------------
If you instantiate a Calendar_Week with the day set to january 1. 2004 and you call $Week->thisYear(), you will see that the year returned will be 2003, because the first day of that week falls in 2003.

Of course, the correct year for week 1 of 2004 would be 2004, not 2003.

Is there a way to get the correct year for the weeks, from which the first days fall in the previous year.

Test script:
---------------
<?php
require_once 'Calendar/Week.php';
require_once 'Calendar/Decorator/Uri.php';
if (!isset($_GET['year']) || !isset($_GET['month']) || !isset($_GET['day'])) { $_GET['year'] = date('Y'); $_GET['month'] = date('n'); $_GET['day'] = date('d'); }
$Week = new Calendar_Week($_GET['year'], $_GET['month'], $_GET['day']);
$Week->build();
$Uri =& new Calendar_Decorator_Uri($Week);
$Uri->setFragments('year', 'month', 'day');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Test</title></head><body><table border="1"><tr><td colspan="7">Week: <?php echo $Week->thisWeek('n_in_year'); ?> of <?php echo $Week->thisYear(); ?></td></tr><tr><td width=80>Monday</td><td width=80>Tuesday</td><td width=80>Wednesday</td><td width=80>Thursday</td><td width=80>Friday</td><td width=80>Saturday</td><td width=80>Sunday</td></tr><tr>
<?php
while ($Day = $Week->fetch()) {
echo "<td>" . $Day->thisYear() . "-" . $Day->thisMonth() . "-" . $Day->thisDay() . "</td>";
}
?>
</tr></table><p>
<?php
$prev = $_SERVER['PHP_SELF'] . "?" . $Uri->prev('week');
$next = $_SERVER['PHP_SELF'] . "?" . $Uri->next('week');
echo "<a href='${prev}'>Previous week</a>  ";
echo "<a href='${_SERVER['PHP_SELF']}?year=".date('Y')."&month=".date('n')."&day=".date('d')."'>Current week</a>  ";
echo "<a href='${next}'>Next week</a>";
?>
<p>Erroneous year:<a href="<?php echo $_SERVER['PHP_SELF']; ?>?year=2003&month=12&day=29">First week of 2004 displays wrong year!</a></body></html>

Expected result:
----------------
The first week of 2004 should display 2004 when calling $Week->thisYear() when using the parameters: year=2003&month=12&day=29.

Actual result:
--------------
The wrong year for the $Week object is returned, because the first day of the $Week-object is in the previous year.

[2006-10-20 15:42 UTC] macristo at gmx dot net

Thanks Lorenzo,

It seems to work alright now!

Regards,

Patrick