Range formatter: if range spans multiple days, display meridian on time format

Created on 18 October 2024, 2 months ago

Problem/Motivation

For a date range spanning from, for example, October 18, 2024 at 10pm to October 20, 2024 and 11pm, the current logic will render this as Oct. 18 to 20, 2024, 10 to 11 p.m..

Since the AP date style guide doesn't specify, from what I can find, how times should be displayed when spanning multiple days, there may be some subjectivity here. Possible improvements:

  1. Oct. 19 at 10 p.m.to Oct 22, 2024 at 11 p.m.. This intuitively feels like the most readable. But it would also be the most divergent from the existing logic in date_ap_style.
  2. Oct. 19 to 22, 2024, 10 p.m. to 11 p.m.. This is only slightly better than the current display, but it would involve the least divergence from the current business logic.

Steps to reproduce

1. Create a date range from October 18, 10:00 p.m. to October 20, 11:00 p.m. and render it with date_ap_style formatter. See above.

Proposed resolution

The minimum solution would be to update the already difficult to parse logic as shown below. However, this could be an opportunity to make that logic easier to read...

diff --git a/src/ApStyleDateFormatter.php b/src/ApStyleDateFormatter.php
index 3ef7eb2..c799db6 100644
--- a/src/ApStyleDateFormatter.php
+++ b/src/ApStyleDateFormatter.php
@@ -471,7 +471,7 @@ class ApStyleDateFormatter {
     $time_start = $start_stamp->format('i') == '00' ? 'g' : 'g:i';
     // If 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.
✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024