ID:               40271
 User updated by:  slomo at sonarkollektiv dot de
 Reported By:      slomo at sonarkollektiv dot de
-Status:           Feedback
+Status:           Open
 Bug Type:         Date/time related
 Operating System: Mac OS X 10.4.8
 PHP Version:      5.2.0
 New Comment:

My "Reproduce code" was quite complete. Here it is again, 
this time it starts with <?php and ends with ?> :-)

I expect: using strtotime("last monday") should behave 
always the same. If you run my code you will see that it 
differs depending on the given date. My code is not complex, 
so I hope you get the point very soon.

This "Day of week items" are a little bit confusing, because 
if you have e.g. a $date for a monday and state strtotime
("$date last monday"), you get $date. I assume this is 
correct.

Anyway I would think it's just my fault if my function would 
be "off 1 week" for the whole year if the date is a monday 
every time. But this happens only for the first week, in any 
other week the result is like I learned strtotime() is doing 
it.

Can you follow?

<?php

function MondayOfWeek($year, $week) { 
        return strtotime("$year-01-04 UTC + " . ($week-1) . ' 
week last monday');
} 

for ($y = 1998; $y < 2012; $y++) {
        for ($w = 1; $w < 53; $w++) {
                $start = MondayOfWeek($y, $w);
                $end   = strtotime("+6 day", $start);
                
                echo "KW $w/$y: " . gmstrftime('%A, %x %X', $start) 
. ' - ' . gmstrftime('%A, %x %X', $end) . ' - reverse check 
(week/year): ' . gmstrftime('%V/%G', $start) . "<br />\n";
                
                if ($w != gmstrftime('%V', $start) || $y != 
gmstrftime('%G', $start)) {
                        echo '<span style="color: red;">Oops, 
EXCEPTIONAL ERROR!</span>' . "<br />\n";
                }
        }
        echo "<hr>\n";
}

?>

best regards,
  slomo


Previous Comments:
------------------------------------------------------------------------

[2007-01-29 11:50:06] [EMAIL PROTECTED]

You can also just use "strtotime('2007W011')"; for Monday "1", of Week
1 ("01") of year 2007 ("2007").

------------------------------------------------------------------------

[2007-01-29 11:33:36] [EMAIL PROTECTED]

And please do not forget about expected and actual results.
Thank you.

------------------------------------------------------------------------

[2007-01-29 11:32:05] [EMAIL PROTECTED]

Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.



------------------------------------------------------------------------

[2007-01-29 11:20:44] slomo at sonarkollektiv dot de

Description:
------------
I was looking for a neat solution for the task "get the date 
for a given weeknumber and weekyear". I thought I got it, but 
there is a strange behaviour if (and only if) week no. is 1 
and 4th of January is already a monday. See my code to 
reproduce the results, it's quite confusing.

The idea was:
- 4th of January is always in week no. 1
- add the missing weeks
- make it a monday of that week acording to the GNU Date Input 
Formats syntax descibed at http://www.gnu.org/software/tar/
manual/html_node/tar_114.html#SEC114

So either me or PHP still does not get it right.

Reproduce code:
---------------
function MondayOfWeek($year, $week) { 
        return strtotime("$year-01-04 UTC + " . ($week-1) . ' week last
monday');
} 

for ($y = 1998; $y < 2012; $y++) {
        for ($w = 1; $w < 53; $w++) {
                $start = MondayOfWeek($y, $w);
                $end   = strtotime("+6 day", $start);
                
                echo "KW $w/$y: " . gmstrftime('%A, %x %X', $start) . ' - ' .
gmstrftime('%A, %x %X', $end) . " ";
                echo '- reverse check (week/year): ' . gmstrftime('%V/%G', 
$start) .
"\n";
                
                if ($w != gmstrftime('%V', $start) || $y != gmstrftime('%G', 
$start))
{
                        echo "ERROR!\n";
                }
        }
        echo "<hr>\n";
}

Expected result:
----------------
accurate results for every week number and year with always 
the same behaviour ...

Actual result:
--------------
if week no. is 1 and 4th of January is already a monday, the 
function returns timestamps for the "previous week".


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=40271&edit=1

Reply via email to