[TriLUG] Perplexed

Rick DeNatale rick.denatale at gmail.com
Tue Apr 4 17:34:05 EDT 2006


Never one to pass up an interesting problem, I tried to do away with
the testing so first I came up with:

<?php

$day = `date +%A`;
/* loop through testing from last Sunday for 1 week */
$weekdays = array('Sunday', 'Monday', 'Tuesday', 'Wednesday',
                  'Thursday', 'Friday');
foreach ($weekdays as $weekday) {
        $ts = strtotime("$weekday");
        $today=date("l F d, Y",$ts);
        print "For $today";
  /* the next two lines do the actual work, everything else is test harness */
        $dayofweek = date("w",$today);
        $lastMon = date("Y-m-d",strtotime("$dayofweek days ago last monday"));
        print "  Last Monday was $lastMon\n";
}
?>

which works, and gets the calculation down to 2 lines, but I was
bothered by the fact that the test dates jump back a week when it got
to today.  So I worked on the test harness and came up with:

?php

$day = `date +%A`;
/* loop through testing from last Sunday for 1 week */
$oneday = strtotime('tomorrow') - strtotime('today');
$monday = strtotime("last Monday");
   /* Needed to specify time since of last sundays dst switch!!! */
$sunday = $monday + 6 * $oneday;
for ($ts = $monday; $ts <= $sunday; $ts += $oneday) {
        $today=date("l F d, Y",$ts);
        print "For $today";
        $dayofweek = date("w",$today);
        $lastMon = date("Y-m-d",strtotime("$dayofweek days ago last monday"));
        print "  Last Monday was $lastMon\n";
}
?>

I'd tried looping through things like
  strtotime("last Sunday +$weekday days")
but that seems to broken, both in php's strtotime and in the linux date command.

I'd also tried to do this from Sunday to Saturday, and ran into a
glitch because, for this week, day("l", strtotime("last Sunday") gives
"Saturday"!
This is because of the DST change.  I just figured out that although
strtotime("last Sunday noon") which is what I originally tried doesn't
work, strtotime("last Sunday 3:00") or the like does so now I've got:

<?php

$day = `date +%A`;
/* loop through testing from last Sunday for 1 week */
$oneday = strtotime('tomorrow') - strtotime('today');
$sunday = strtotime("last Sunday 3:00");
   /* Needed to specify time since of last sundays dst switch!!! */
$saturday = $sunday + 6 * $oneday;
for ($ts = $sunday; $ts <= $saturday; $ts += $oneday) {
        $today=date("l F d, Y",$ts);
        print "For $today";
        $dayofweek = date("w",$today);
        $lastMon = date("Y-m-d",strtotime("$dayofweek days ago last monday"));
        print "  Last Monday was $lastMon\n";
}
?>

Having been through this, I have to say that I'm not that happy with
the gnu date format which is used by strtotime and the --date
parameter to the date command.  It just feels a little loosey goosey
in the specification to me.

It's also interesting to contemplate how long it would have taken to
realise the DST bug in the test harness code if I hadn't happened to
have been doing this THIS week, or what other similar bugs might be
hiding here.


--
Rick DeNatale

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/



More information about the TriLUG mailing list