From 35248732ce30f6f36a09528aa9a5eb48407d83ae Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 1 Mar 2017 17:40:57 +0100 Subject: [PATCH] Added helper functions --- Datatypes/SmartDateTime.php | 80 +++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/Datatypes/SmartDateTime.php b/Datatypes/SmartDateTime.php index 50c94bf04..c098a8553 100644 --- a/Datatypes/SmartDateTime.php +++ b/Datatypes/SmartDateTime.php @@ -118,11 +118,27 @@ class SmartDateTime extends \DateTime return $this; } + /** + * Get end of month object + * + * @return SmartDateTime + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getEndOfMonth() : SmartDateTime { return new SmartDateTime($this->format('Y') . '-' . $this->format('m') . '-' . $this->getDaysOfMonth()); } + /** + * Get start of month object + * + * @return SmartDateTime + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getStartOfMonth() : SmartDateTime { return new SmartDateTime($this->format('Y') . '-' . $this->format('m') . '-01'); @@ -154,4 +170,68 @@ class SmartDateTime extends \DateTime return getdate(mktime(0, 0, 0, (int) $this->format('m'), 1, (int) $this->format('Y')))['wday']; } + /** + * Is leap year in gregorian calendar + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function isLeapYear() : bool + { + return self::leapYear((int) $this->format('Y')); + } + + /** + * Test year if leap year in gregorian calendar + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function leapYear(int $year) : bool + { + $isLeap = false; + + if ($year % 4 == 0) { + $isLeap = true; + } + + if ($year % 100 == 0) { + $isLeap = false; + } + + if ($year % 400 == 0) { + $isLeap = true; + } + + return $isLeap; + } + + /** + * Get day of week + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDayOfWeek(int $y, int $m, int $d) : int + { + $w = 1; + $y = ($y - 1) % 400 + 1; + $ly = ($y - 1) / 4; + $ly = $ly - ($y - 1) / 100; + $ly = $ly + ($y - 1) / 400; + $ry = $y - 1 - $ly; + $w = $w + $ry; + $w = $w + 2 * $ly; + $w = $w + date("z", mktime(0, 0, 0, $m, $d, $y)) + 1;; + $w = ($w - 1) % 7 + 1; + + return $w; + } + }