Recipe 3.16. Using Dates Outside the Range of an Epoch Timestamp

3.16.1. Problem

You want to use dates that are outside the range of what a 32-bit epoch timestamp can handle: roughly before 1901 or after 2038.

3.16.2. Solution

Use the PEAR Date_Calc class, which can handle dates from January 1, 1 CE to December 31, 9999 CE. Example 3-45 prints formatted dates for two days in the 9th century CE.

Using Date_Calc

<?php require_once 'Date/Calc.php'; // April 17, 1790 $date = Date_Calc::dateFormat( 17, 4, 1790, '%A %B %e, %Y'); print "Benjamin Franklin died on $date."; ?>

Example 3-45 prints:

Benjamin Franklin died on Saturday April 17, 1790.

3.16.3. Discussion

Because Date_Calc uses its own internal representation for dates, it's not subject to the limits of storing an epoch timestamp in a 32-bit integer. Its dateFormat( ) method works similarly to strftime( )'it turns a format string into a formatted date and time string. Table 3-7 lists the formatting characters that dateFormat( ) understands.

Table Formatting characters for Date_Calc::dateFormat( )




Day of month, with leading 0


Day of month, no leading 0


Day of week, no leading 0, Sunday is 0


Day of year, with leading 0


Day count according to internal Date_Calc epoch


Weekday name, short


Weekday name, full


Week number of current year


Month number, no leading 0, January is 1


Month name, short


Month name, long


Year, 2-digit with leading 0


Year, 4-digit with leading 0







Date_Calc makes it easy to work with a wide range of Gregorian calendar dates, but it does not have comprehensive knowledge of the religious, political, and cultural factors that have caused modification to the calendar over time.

3.16.4. See Also

The PEAR Date package at The tip of the calendar-changing-over-time-wackiness iceberg is explored at

PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: