function gregorianToISO($day, $month, $year) { $mnth = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334); $y_isleap = Date_Calc::isLeapYear($year); $y_1_isleap = Date_Calc::isLeapYear($year - 1); $day_of_year_number = $day + $mnth[$month - 1]; if ($y_isleap && $month > 2) { $day_of_year_number++; } // find Jan 1 weekday (monday = 1, sunday = 7) $yy = ($year - 1) % 100; $c = $year - 1 - $yy; $g = $yy + intval($yy / 4); $jan1_weekday = 1 + intval(($c / 100 % 4 * 5 + $g) % 7); // weekday for year-month-day $h = $day_of_year_number + ($jan1_weekday - 1); $weekday = 1 + intval(($h - 1) % 7); // find if Y M D falls in YearNumber Y-1, WeekNumber 52 or if ($day_of_year_number <= 8 - $jan1_weekday && $jan1_weekday > 4) { $yearnumber = $year - 1; if ($jan1_weekday == 5 || $jan1_weekday == 6 && $y_1_isleap) { $weeknumber = 53; } else { $weeknumber = 52; } } else { $yearnumber = $year; } // find if Y M D falls in YearNumber Y+1, WeekNumber 1 if ($yearnumber == $year) { if ($y_isleap) { $i = 366; } else { $i = 365; } if ($i - $day_of_year_number < 4 - $weekday) { $yearnumber++; $weeknumber = 1; } } // find if Y M D falls in YearNumber Y, WeekNumber 1 through 53 if ($yearnumber == $year) { $j = $day_of_year_number + (7 - $weekday) + ($jan1_weekday - 1); //$weeknumber = intval($j / 7) + 1; // kludge!!! - JMC $weeknumber = intval($j / 7); // kludge!!! - JMC if ($jan1_weekday > 4) { $weeknumber--; } } // put it all together if ($weeknumber < 10) { $weeknumber = '0' . $weeknumber; } return "{$yearnumber}-{$weeknumber}-{$weekday}"; }
/** * Determine if the year in this date is a leap year * * Determine if the year in this date is a leap year * * @access public * @return boolean true if this year is a leap year */ function isLeapYear() { return Date_Calc::isLeapYear($this->year); }
/** * Find the number of days in the given month * * @param int $month the month, default is current local month * @param int $year the year in four digit format, default is current local year * * @return int the number of days the month has * * @access public * @static */ function daysInMonth($month = 0, $year = 0) { if (empty($year)) { $year = Date_Calc::dateNow('%Y'); } if (empty($month)) { $month = Date_Calc::dateNow('%m'); } if ($year == 1582 && $month == 10) { return 21; // October 1582 only had 1st-4th and 15th-31st } if ($month == 2) { if (Date_Calc::isLeapYear($year)) { return 29; } else { return 28; } } elseif ($month == 4 or $month == 6 or $month == 9 or $month == 11) { return 30; } else { return 31; } }
/** * Find the number of days in the given month. * * @param string month in format MM, default current local month * * @access public * * @return int number of days */ function daysInMonth($month = "", $year = "") { if (empty($year)) { $year = Date_Calc::dateNow("%Y"); } if (empty($month)) { $month = Date_Calc::dateNow("%m"); } if ($month == 2) { if (Date_Calc::isLeapYear($year)) { return 29; } else { return 28; } } elseif ($month == 4 or $month == 6 or $month == 9 or $month == 11) { return 30; } else { return 31; } }
/** * Expands the planned transactions. * * All occurences of planned transactions between now and the targetFutureCalcDate will be inserted * in finishedTransactions. For distinction the planned transactions will have a 'p' as first character * in their id. * * @throws BadgerException If an illegal repeat unit is used. */ public function expandPlannedTransactions() { $now = new Date(); $now->setHour(0); $now->setMinute(0); $now->setSecond(0); foreach ($this->plannedTransactions as $currentTransaction) { $date = new Date($currentTransaction->getBeginDate()); $dayOfMonth = $date->getDay(); //While we have not reached targetFutureCalcDate while ($this->targetFutureCalcDate->after($date) && !$date->after(is_null($tmp = $currentTransaction->getEndDate()) ? new Date('9999-12-31') : $tmp)) { $inRange = true; //Check if there is one or more valutaDate filter and apply them foreach ($this->filter as $currentFilter) { if ($currentFilter['key'] == 'valutaDate') { switch ($currentFilter['op']) { case 'eq': if (Date::compare($date, $currentFilter['val']) != 0) { $inRange = false; } break; case 'lt': if (Date::compare($date, $currentFilter['val']) >= 0) { $inRange = false; } break; case 'le': if (Date::compare($date, $currentFilter['val']) > 0) { $inRange = false; } break; case 'gt': if (Date::compare($date, $currentFilter['val']) <= 0) { $inRange = false; } break; case 'ge': if (Date::compare($date, $currentFilter['val']) < 0) { $inRange = false; } break; case 'ne': if (Date::compare($date, $currentFilter['val']) == 0) { $inRange = false; } break; case 'bw': case 'ew': case 'ct': if (strncasecmp($date->getFormatted(), $currentFilter['val']->getFormatted(), 9999) != 0) { $inRange = false; } break; } if (!$inRange) { break; } } } if (!$date->before($now) && $inRange) { $this->finishedTransactions[] = new FinishedTransaction($this->badgerDb, $this, 'p' . $currentTransaction->getId() . '_' . $date->getDate(), $currentTransaction->getTitle(), $currentTransaction->getAmount(), $currentTransaction->getDescription(), new Date($date), $currentTransaction->getTransactionPartner(), $currentTransaction->getCategory(), $currentTransaction->getOutsideCapital(), false, true, $currentTransaction, 'PlannedTransaction'); } //do the date calculation switch ($currentTransaction->getRepeatUnit()) { case 'day': $date->addSeconds($currentTransaction->getRepeatFrequency() * 24 * 60 * 60); break; case 'week': $date->addSeconds($currentTransaction->getRepeatFrequency() * 7 * 24 * 60 * 60); break; case 'month': //Set the month $date = new Date(Date_Calc::endOfMonthBySpan($currentTransaction->getRepeatFrequency(), $date->getMonth(), $date->getYear(), '%Y-%m-%d')); //And count back as far as the last valid day of this month while ($date->getDay() > $dayOfMonth) { $date->subtractSeconds(24 * 60 * 60); } break; case 'year': $newYear = $date->getYear() + $currentTransaction->getRepeatFrequency(); if ($dayOfMonth == 29 && $date->getMonth() == 2 && !Date_Calc::isLeapYear($newYear)) { $date->setDay(28); } else { $date->setDay($dayOfMonth); } $date->setYear($newYear); break; default: throw new BadgerException('Account', 'IllegalRepeatUnit', $currentTransaction->getRepeatUnit()); exit; } } } }
function transferFinishedTransactions($account, $plannedTransaction) { $now = new Date(); $date = new Date($plannedTransaction->getBeginDate()); $dayOfMonth = $date->getDay(); //While we are before now and the end date of this transaction while (!$date->after($now) && !$date->after(is_null($tmp = $plannedTransaction->getEndDate()) ? new Date('9999-12-31') : $tmp)) { $account->addFinishedTransaction($plannedTransaction->getAmount(), $plannedTransaction->getTitle(), $plannedTransaction->getDescription(), new Date($date), $plannedTransaction->getTransactionPartner(), $plannedTransaction->getCategory(), $plannedTransaction->getOutsideCapital(), false, true); //do the date calculation switch ($plannedTransaction->getRepeatUnit()) { case 'day': $date->addSeconds($plannedTransaction->getRepeatFrequency() * 24 * 60 * 60); break; case 'week': $date->addSeconds($plannedTransaction->getRepeatFrequency() * 7 * 24 * 60 * 60); break; case 'month': //Set the month $date = new Date(Date_Calc::endOfMonthBySpan($plannedTransaction->getRepeatFrequency(), $date->getMonth(), $date->getYear(), '%Y-%m-%d')); //And count back as far as the last valid day of this month while ($date->getDay() > $dayOfMonth) { $date->subtractSeconds(24 * 60 * 60); } break; case 'year': $newYear = $date->getYear() + $plannedTransaction->getRepeatFrequency(); if ($dayOfMonth == 29 && $date->getMonth() == 2 && !Date_Calc::isLeapYear($newYear)) { $date->setDay(28); } else { $date->setDay($dayOfMonth); } $date->setYear($newYear); break; default: throw new BadgerException('Account', 'IllegalRepeatUnit', $plannedTransaction->getRepeatUnit()); exit; } } }
private function previousOccurence($date, $start = null) { if (is_null($start)) { $start = $this->beginDate; } $dayOfMonth = $start->getDay(); //do the date calculation switch ($this->repeatUnit) { case 'day': $date->subtractSeconds($this->repeatFrequency * 24 * 60 * 60); break; case 'week': $date->subtractSeconds($this->repeatFrequency * 7 * 24 * 60 * 60); break; case 'month': //Set the month $date = new Date(Date_Calc::endOfMonthBySpan(-$this->repeatFrequency, $date->getMonth(), $date->getYear(), '%Y-%m-%d')); //And count back as far as the last valid day of this month while ($date->getDay() > $dayOfMonth) { $date->subtractSeconds(24 * 60 * 60); } break; case 'year': $newYear = $date->getYear() - $this->repeatFrequency; if ($dayOfMonth == 29 && $date->getMonth() == 2 && !Date_Calc::isLeapYear($newYear)) { $date->setDay(28); } else { $date->setDay($dayOfMonth); } $date->setYear($newYear); break; default: throw new BadgerException('Account', 'IllegalRepeatUnit', $this->repeatUnit); exit; } //switch return $date; }
function transferFormerFinishedTransactions($account) { global $us; if ($us->getProperty('autoExpandPlannedTransactions') == false) { return; } $now = new Date(); $now->setHour(0); $now->setMinute(0); $now->setSecond(0); $account->setType('planned'); $account->setFilter(array(array('key' => 'beginDate', 'op' => 'le', 'val' => $now))); try { $lastInsertDate = $us->getProperty('Account_' . $account->getId() . '_LastTransferFormerFinishedTransactions'); } catch (BadgerException $ex) { $lastInsertDate = new Date('1000-01-01'); } $us->setProperty('Account_' . $account->getId() . '_LastTransferFormerFinishedTransactions', $now); if (!$lastInsertDate->before($now)) { return; } while ($currentTransaction = $account->getNextPlannedTransaction()) { $date = new Date($currentTransaction->getBeginDate()); $dayOfMonth = $date->getDay(); //While we are before now and the end date of this transaction while (!$date->after($now) && !$date->after(is_null($tmp = $currentTransaction->getEndDate()) ? new Date('9999-12-31') : $tmp)) { if ($date->after($lastInsertDate)) { $account->addFinishedTransaction($currentTransaction->getAmount(), $currentTransaction->getTitle(), $currentTransaction->getDescription(), new Date($date), $currentTransaction->getTransactionPartner(), $currentTransaction->getCategory(), $currentTransaction->getOutsideCapital(), false, true); } //do the date calculation switch ($currentTransaction->getRepeatUnit()) { case 'day': $date->addSeconds($currentTransaction->getRepeatFrequency() * 24 * 60 * 60); break; case 'week': $date->addSeconds($currentTransaction->getRepeatFrequency() * 7 * 24 * 60 * 60); break; case 'month': //Set the month $date = new Date(Date_Calc::endOfMonthBySpan($currentTransaction->getRepeatFrequency(), $date->getMonth(), $date->getYear(), '%Y-%m-%d')); //And count back as far as the last valid day of this month while ($date->getDay() > $dayOfMonth) { $date->subtractSeconds(24 * 60 * 60); } break; case 'year': $newYear = $date->getYear() + $currentTransaction->getRepeatFrequency(); if ($dayOfMonth == 29 && $date->getMonth() == 2 && !Date_Calc::isLeapYear($newYear)) { $date->setDay(28); } else { $date->setDay($dayOfMonth); } $date->setYear($newYear); break; default: throw new BadgerException('Account', 'IllegalRepeatUnit', $currentTransaction->getRepeatUnit()); exit; } } } }
compare('20050126', Date_Calc::NWeekdayOfMonth('last', 3, 1, 2005), 'NWeekdayOfMonth l31'); compare('20050125', Date_Calc::NWeekdayOfMonth('last', 2, 1, 2005), 'NWeekdayOfMonth l21'); compare('20050331', Date_Calc::NWeekdayOfMonth('last', 4, 3, 2005), 'NWeekdayOfMonth l43'); compare('20050330', Date_Calc::NWeekdayOfMonth('last', 3, 3, 2005), 'NWeekdayOfMonth l33'); compare('20050329', Date_Calc::NWeekdayOfMonth('last', 2, 3, 2005), 'NWeekdayOfMonth l23'); compare('20050328', Date_Calc::NWeekdayOfMonth('last', 1, 3, 2005), 'NWeekdayOfMonth l13'); compare('20050327', Date_Calc::NWeekdayOfMonth('last', 0, 3, 2005), 'NWeekdayOfMonth l03'); compare('20050326', Date_Calc::NWeekdayOfMonth('last', 6, 3, 2005), 'NWeekdayOfMonth l63'); compare('20050325', Date_Calc::NWeekdayOfMonth('last', 5, 3, 2005), 'NWeekdayOfMonth l53'); compare(false, Date_Calc::isValidDate(29, 2, 1900), 'isValidDate 1'); compare(true, Date_Calc::isValidDate(29, 2, 2000), 'isValidDate 2'); compare(true, Date_Calc::isValidDate('29', '02', '2000'), 'isValidDate 2 str'); compare(false, Date_Calc::isLeapYear(1900), 'isLeapYear 1'); compare(true, Date_Calc::isLeapYear(1996), 'isLeapYear 2'); compare(true, Date_Calc::isLeapYear(2000), 'isLeapYear 3'); compare(false, Date_Calc::isLeapYear(2001), 'isLeapYear 4'); compare(false, Date_Calc::isLeapYear('2001'), 'isLeapYear 4 str'); compare(false, Date_Calc::isFutureDate('22', '11', '2000'), 'isFutureDate 1 str'); compare(false, Date_Calc::isFutureDate(22, 11, 2000), 'isFutureDate 1'); compare(true, Date_Calc::isFutureDate(22, 11, date('Y') + 1), 'isFutureDate 2'); compare(false, Date_Calc::isPastDate(22, 11, date('Y') + 1), 'isPastDate 1'); compare(true, Date_Calc::isPastDate(22, 11, 2000), 'isPastDate 2'); compare(true, Date_Calc::isPastDate('22', '11', '2000'), 'isPastDate 2 str'); compare(10, Date_Calc::dateDiff(22, 11, 2000, 12, 11, 2000), 'dateDiff 1'); compare(10, Date_Calc::dateDiff(12, 11, 2000, 22, 11, 2000), 'dateDiff 2'); compare(61, Date_Calc::dateDiff(22, 11, 2000, 22, 1, 2001), 'dateDiff 3'); compare(61, Date_Calc::dateDiff('22', '11', '2000', '22', '01', '2001'), 'dateDiff 3 str'); compare(-1, Date_Calc::compareDates(12, 11, 2000, 22, 11, 2000), 'compareDates 1'); compare(0, Date_Calc::compareDates(22, 11, 2000, 22, 11, 2000), 'compareDates 2'); compare(1, Date_Calc::compareDates(22, 11, 2000, 12, 11, 2000), 'compareDates 3'); compare(1, Date_Calc::compareDates('22', '11', '2000', '12', '11', '2000'), 'compareDates 3 str');