Created
March 25, 2025 11:28
-
-
Save JohnRDOrazio/577a276a91d756cb68579f53a10765a3 to your computer and use it in GitHub Desktop.
PHP DATE_ISO8601_EXPANDED proof of concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* In this example we test formatting a date beyond 9999, | |
* both with `DateTime->format(DATE_ISO8601_EXPANDED)` | |
* and with `IntlDateFormatter::create`. | |
* Since we cannot create a date beyond the year 9999 | |
* based on a textual representation, | |
* we manually calculate the timestamp for our extended date. | |
* Let's say we want to get the date May 11 20030, | |
* we start with a base date May 11 1970 at zero hours UTC | |
* and get the timestamp of this date, | |
* then manually calculate the seconds to add in order to | |
* get the timestamp for our extended date. | |
* In this example we see that we can actually still modify | |
* the date object with DateTime->modify(), | |
* so instead of manually calculating the seconds | |
* we could use DateTime->modify to achieve our future extended date. | |
* We can even format it correctly using IntlDateFormatter::create. | |
* So the only trouble is parsing a future extended date from a string, | |
* but there is no trouble handling modifies or comparisons or the likes, | |
* and there is no trouble with formatting the date for output. | |
*/ | |
function countLeapYears($start_year, $end_year) { | |
return floor($end_year / 4) - floor(($start_year - 1) / 4) | |
- floor($end_year / 100) + floor(($start_year - 1) / 100) | |
+ floor($end_year / 400) - floor(($start_year - 1) / 400); | |
} | |
$baseTimestamp = strtotime('1970-05-11 00:00:00 UTC'); | |
// Calculate the number of seconds from 1970 to 20030 | |
$yearsDifference = 20030 - 1970; | |
$secondsPerDay = 24 * 60 * 60; | |
$secondsPerYear = 365 * $secondsPerDay; // Year length in seconds | |
$additionalSeconds = $yearsDifference * $secondsPerYear; | |
$leapYears = countLeapYears(1970, 20030); | |
$additionalSeconds += ($leapYears * $secondsPerDay); // account for leap years | |
// Add the additional seconds to the base timestamp | |
$futureTimestamp = $baseTimestamp + $additionalSeconds; | |
$date = new DateTime(); | |
$date->setTimestamp($futureTimestamp); | |
$date->modify('+1 day'); | |
echo $date->format(DATE_ISO8601_EXPANDED); | |
$formatter = IntlDateFormatter::create( | |
'it_IT', | |
IntlDateFormatter::FULL, | |
IntlDateFormatter::NONE, | |
'UTC' | |
); | |
echo PHP_EOL; | |
echo $formatter->format($futureTimestamp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment