From: Iain Patterson Date: Thu, 13 Mar 2014 19:05:08 +0000 (-0400) Subject: Fix incorrect week count across year boundary. X-Git-Tag: 2014-03-13^0 X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;ds=sidebyside;h=e4e05bf5dad3c8e514c5a939867758170ba36dd7;hp=-c;p=readifood.git Fix incorrect week count across year boundary. Year/week 201401 is only one week later than 201352, not 49. --- e4e05bf5dad3c8e514c5a939867758170ba36dd7 diff --git a/lib/report.php b/lib/report.php index f06e8ca..b2bc16d 100644 --- a/lib/report.php +++ b/lib/report.php @@ -69,15 +69,27 @@ $q->withColumn('count(*)', 'count'); $q->addGroupByColumn('week')->orderByUpdated(); $rows = $q->find(); + $year_offset = 0; $week_offset = 0; $week = 1; $last_week = 0; $total = 0; echo "\n"; foreach ($rows as $row) { - /* Convert week of year to date range. */ - if (! $week_offset) $week_offset = $row->getWeek() - 1; - else $week = $row->getWeek() - $week_offset; + /* + Convert week of year to date range. + Beware that week 201401 comes after 201352. + */ + $yearweek = $row->getWeek(); + if (! $week_offset) { + $year_offset = substr($yearweek, 0, 4); + $week_offset = substr($yearweek, 4, 2) - 1; + } + else { + $y = substr($yearweek, 0, 4); + $w = substr($yearweek, 4, 2); + $week = (($y - $year_offset) * 52) + ($w - $week_offset); + } $total += $row->getCount(); /* Fill in missing weeks. XXX */ for ($missing_week = $last_week + 1; $missing_week < $week; $missing_week++) {