diff -u b/core/lib/Drupal/Core/Datetime/DateHelper.php b/core/lib/Drupal/Core/Datetime/DateHelper.php --- b/core/lib/Drupal/Core/Datetime/DateHelper.php +++ b/core/lib/Drupal/Core/Datetime/DateHelper.php @@ -453,10 +453,13 @@ * The number of days in the month. */ public static function daysInMonth($date = NULL) { - if ($date && !$date instanceof DrupalDateTime) { + if (!isset($date) || empty($date)) { + $date = new DrupalDateTime(); + } + elseif (!$date instanceof DrupalDateTime) { $date = new DrupalDateTime($date); } - if ($date && !$date->hasErrors()) { + if (!$date->hasErrors()) { return $date->format('t'); } return NULL; @@ -473,10 +476,13 @@ * The number of days in the year. */ public static function daysInYear($date = NULL) { - if ($date && !$date instanceof DrupalDateTime) { + if (!isset($date) || empty($date)) { + $date = new DrupalDateTime(); + } + elseif (!$date instanceof DrupalDateTime) { $date = new DrupalDateTime($date); } - if ($date && !$date->hasErrors()) { + if (!$date->hasErrors()) { if ($date->format('L')) { return 366; } @@ -498,10 +504,13 @@ * The number of the day in the week. */ public static function dayOfWeek($date = NULL) { - if ($date && !$date instanceof DrupalDateTime) { + if (!isset($date) || empty($date)) { + $date = new DrupalDateTime(); + } + elseif (!$date instanceof DrupalDateTime) { $date = new DrupalDateTime($date); } - if ($date && !$date->hasErrors()) { + if (!$date->hasErrors()) { return $date->format('w'); } return NULL; @@ -521,10 +530,13 @@ * The name of the day in the week for that date. */ public static function dayOfWeekName($date = NULL, $abbr = TRUE) { - if ($date && !$date instanceof DrupalDateTime) { + if (!isset($date) || empty($date)) { + $date = new DrupalDateTime(); + } + elseif (!$date instanceof DrupalDateTime) { $date = new DrupalDateTime($date); } - if ($date && !$date->hasErrors()) { + if (!$date->hasErrors()) { $dow = self::dayOfWeek($date); $days = $abbr ? self::weekDaysAbbr() : self::weekDays(); return $days[$dow]->getUntranslatedString(); diff -u b/core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php b/core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php --- b/core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php +++ b/core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php @@ -164,7 +164,7 @@ */ public function testDaysInMonth() { // Pass nothing and expect to get NULL. - $this->assertNull(DateHelper::daysInMonth()); + $this->assertEquals(date('t'), DateHelper::daysInMonth()); // December 31st 2022 is a Saturday. $value = '2022-12-31 00:00:00'; @@ -182,7 +182,7 @@ */ public function testDaysInYear() { // Pass nothing and expect to get NULL. - $this->assertNull(DateHelper::daysInYear()); + $this->assertContains(DateHelper::daysInYear(), [365, 366]); // December 31st 2022 is a Saturday. $value = '2022-12-31 00:00:00'; @@ -200,7 +200,7 @@ */ public function testDayOfWeek() { // Pass nothing and expect to get NULL. - $this->assertNull(DateHelper::dayOfWeek()); + $this->assertContains(DateHelper::dayOfWeek(), range(1, 7)); // December 31st 2022 is a Saturday. $value = '2022-12-31 00:00:00'; @@ -218,7 +218,8 @@ */ public function testDayOfWeekName() { // Pass nothing and expect to get NULL. - $this->assertNull(DateHelper::dayOfWeekName()); + $days_of_week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; + $this->assertContains(DateHelper::dayOfWeekName(), $days_of_week); // December 31st 2022 is a Saturday. $value = '2022-12-31 00:00:00';