Skip to content

Provides precise calendar-aware date intervals (leap-year accurate) for birthdays and time-ago displays.

License

Notifications You must be signed in to change notification settings

backdrop-contrib/date_precision

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Date Precision

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.

Features

  • 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.

Installation

Install this module using the official Backdrop CMS instructions at https://docs.backdropcms.org/documentation/extend-with-modules

Configuration / Usage Notes

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.

Developer Usage

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);

Issues:

Bugs and Feature requests should be reported in the Issue Queue: https://github.com/backdrop-contrib/date_precision/issues

Current Maintainer(s):

Credits:

  • 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.

Sponsorship:

License

This project is GPL v2 software. See the LICENSE.txt file in this directory for complete text.

About

Provides precise calendar-aware date intervals (leap-year accurate) for birthdays and time-ago displays.

Resources

License

Stars

Watchers

Forks

Packages

No packages published