diff --git a/src/ApStyleDateFormatter.php b/src/ApStyleDateFormatter.php index 3ef7eb266772e605c2eb89291c491f6dd189273a..20f4b3fa8d5e011ae9d7a5957a968fb76bcb500c 100644 --- a/src/ApStyleDateFormatter.php +++ b/src/ApStyleDateFormatter.php @@ -469,9 +469,9 @@ class ApStyleDateFormatter { // Don't display the minutes if it's the top of the hour. $time_start = $start_stamp->format('i') == '00' ? 'g' : 'g:i'; - // If same start/end meridians and different start/end time, + // If same day, same start/end meridians and different start/end time, // don't include meridian in start. - $time_start .= ($start_stamp->format('a') == $end_stamp->format('a') && $start_stamp->format('gia') != $end_stamp->format('gia') ? '' : ' a'); + $time_start .= ($start_stamp->format('a') == $end_stamp->format('a') && $start_stamp->format('gia') != $end_stamp->format('gia') && $end_stamp->format('Y-m-d') === $start_stamp->format('Y-m-d') ? '' : ' a'); // Set preformatted start and end times based on. // Replace 12:00 am with Midnight & 12:00 pm with Noon. diff --git a/tests/src/Kernel/ApStyleDateFormatterTest.php b/tests/src/Kernel/ApStyleDateFormatterTest.php index 70a681f94482d67cc5f0a30d1d183ab4d237ed70..561ff170596f9409d1e15ab8856745b43e8034bf 100644 --- a/tests/src/Kernel/ApStyleDateFormatterTest.php +++ b/tests/src/Kernel/ApStyleDateFormatterTest.php @@ -485,4 +485,27 @@ class ApStyleDateFormatterTest extends KernelTestBase { $this->assertEquals('Jan. 10 to 20, 2012', $result); } + /** + * Tests format for same day, same meridian, different start/end time. + */ + public function testSameDaySameMeridianDifferentTimes() { + $options = ['display_time' => TRUE, 'time_before_date' => TRUE]; + + // Set up a date range on the same day with the same meridian. + $start = new DrupalDateTime('2023-12-15 10:00:00'); // 10:00 AM + $end = new DrupalDateTime('2023-12-15 11:00:00'); // 11:00 AM + + $timestamps = [ + 'start' => $start->getTimestamp(), + 'end' => $end->getTimestamp(), + ]; + + // Format the date range. + $result = $this->dateFormatter->formatRange($timestamps, $options); + + // Assert that the formatted result omits the meridian from the start time. + $this->assertEquals('10 to 11 a.m., Dec. 15, 2023', $result); + } } + +