diff --git a/class/ukTaxWeek.php b/class/ukTaxWeek.php index 0d3151c..74c36e9 100644 --- a/class/ukTaxWeek.php +++ b/class/ukTaxWeek.php @@ -1,5 +1,5 @@ $cMonth) { - $scenario = 1; - } else if ($tMonth == $cMonth) { - if ($tDay >= $cDay) - { - $scenario = 1; - } else { - $scenario = 2; - } - } else { - $scenario = 2; - } + //convert the provided date to a query string/timestamp + $qDate = strtotime($wDate); - + //Calculate This Day, This Month and This Year from current datetime + $tDay = date('d', $qDate); + $tMonth = date('m', $qDate); + $tYear = date('Y', $qDate); + //Calculate Comparison Day and Comparison Month from floorDate + $floorBits = explode('/', $floorDate); + $cDay = $floorBits[0]; + $cMonth = $floorBits[1]; - if($scenario == 1) - { - //Today is in year 1 - Start of the tax year is in this calendar year - $offset = date("W",strtotime($floorDate . "/" . $tYear)); - //Week 1 was already $offset weeks into the year. Calculate tax week accordingly - $taxWeekNumber = date("W",$qDate) - ($offset - 2); + $floorDate = $floorBits[1].'/'.$floorBits[0]; + + //decide whether the start of the tax year for the given date was this calendar year or last + if ((int) $tMonth > (int) $cMonth) { + $scenario = 1; + } elseif ($tMonth == $cMonth) { + if ($tDay >= $cDay) { + $scenario = 1; + } else { + $scenario = 2; + } + } elseif ($tMonth < $cMonth) { + $scenario = 2; + } + + if ($scenario == 1) { + //Today is in year 1 - Start of the tax year is in this calendar year + $offset = date('W', strtotime($floorDate.'/'.$tYear)); + //Week 1 was already $offset weeks into the year. Calculate tax week accordingly + $taxWeekNumber = date('W', $qDate) - ($offset - 2) - 1; + } elseif ($scenario == 2) { + //Today is in year 0 - Start of the tax year was in last calendar year + $fullYearWeeks = $this->getIsoWeeksInYear($tYear - 1); - } elseif ($scenario == 2) { + $floorWeek = date('W', strtotime($floorDate.'/'.($tYear - 1))); - //Today is in year 0 - Start of the tax year was in last calendar year - $fullYearWeeks = date("W",strtotime("31/12/" . ($tYear - 1))); - $floorWeek = date("W",strtotime($floorDate . "/" . ($tYear - 1))); - //subtract the base week number from the whole year to account for 53 week years. - $offset = $fullYearWeeks - $floorWeek; - //calculate previous years weeks (offset) + this year's week count - $taxWeekNumber = $offset + date("W",$qDate); + //subtract the base week number from the whole year to account for 53 week years. + $offset = $fullYearWeeks - $floorWeek; + + //calculate previous years weeks (offset) + this year's week count + $taxWeekNumber = $offset + date('W', strtotime("$tMonth/$tDay/$tYear")) - 1; + } + + return $taxWeekNumber; + } + + public function getIsoWeeksInYear($year) + { + $date = new DateTime(); + $date->setISODate($year, 53); + return $date->format('W') === '53' ? 53 : 52; } - - return $taxWeekNumber; - } - } -?>