Date Precision is a standalone utility module that provides calendar-accurate date intervals. It was developed to solve the "leap year drift" inherent in core's format_interval() function, ensuring that long-term spans—such as birthdays are calculated correctly according to the actual calendar.
Problem Statement Backdrop Core's format_interval() calculates a year as a fixed block of 31,536,000 seconds (365 days). Over many decades, this math fails to account for leap years, causing the calculated age to drift by several days. For example, a birthdate in 1944 would report an age difference of several days off when using static math.
The Solution This module utilises PHP's DateTime::diff() and DateInterval classes. Instead of dividing total seconds by fixed numbers, it performs calendar-aware comparisons that respect:
- Leap years (e.g., Feb 29th).
- Varying lengths of months (28, 30, or 31 days).
- Sequential granularity without unit gaps.
- Precise Field Formatter: A new formatter called "Age / Birthday (Precise)" available for Date, Timestamp, and Datestamp fields.
- Custom Granularity: Choose between 1 and 7 units of precision (Years, Months, Weeks, Days, Hours, Minutes, Seconds).
- Flexible Display Styles:
-
- Interval: "81 years 11 months"
-
- Time ago: "81 years 11 months ago"
-
- Time hence: "81 years 11 months hence"
-
- Time span: Automatically switches between "ago" and "hence" based on the date.
- Sequential Logic: Unlike core, if you request a granularity of 4, the module will show "0 days" or "0 weeks" to maintain a logical sequence if larger units have already been displayed.
Install this module using the official Backdrop CMS instructions at https://docs.backdropcms.org/documentation/extend-with-modules
Go to the "Manage Display" tab of any content type or a Views configuration.
Select Age / Birthday (Precise) as the formatter for your date field.
Click the gear icon to adjust granularity and display style.
You can use the precision engine in your own modules by calling the internal helper function:
$timestamp = 16789000; // Example timestamp
$precision = 2; // Show 2 units
echo date_precision_format_interval($timestamp, $precision);Bugs and Feature requests should be reported in the Issue Queue: https://github.com/backdrop-contrib/date_precision/issues
- Steve Moorhouse (albanycomputers) (https://github.com/albanycomputers)
- Additional maintainers and contributors welcomed.
- Steve Moorhouse - Zulip (DrAlbany)
- Google Gemini 3.0 assisted with the coding of this module.
- This module was created to address Backdrop Issue #7069 and serves as a reference implementation for future core improvements.
- Albany Computer Services (https://www.albany-computers.co.uk)
- Albany Web Design (https://www.albanywebdesign.co.uk)
- Albany Hosting (https://www.albany-hosting.co.uk)
This project is GPL v2 software. See the LICENSE.txt file in this directory for complete text.