Specifications

Now, the slightly tricky partto convert this time period back to a more human-friendly unit
of measure. This is not a time stamp but instead the age of the person measured in seconds. We
can convert it back to years by dividing by the number of seconds in a year. We then round it
down using the floor() function as a person is not said to be, for example 20, until the end of
his twentieth year:
$age = floor($ageunix / (365 * 24 * 60 * 60)); // convert from seconds to years
Note, however, that this approach is somewhat flawed as it is limited by the range of UNIX
time stamps (generally 32-bit integers).
Using the Calendar Functions
PHP has a set of functions that enables you to convert between different calendar systems. The
main calendars you will work with are the Gregorian, Julian, and the Julian Day Count.
The Gregorian calendar is the one most Western countries currently use. The Gregorian date
October 15, 1582 is equivalent to October 5, 1582, in the Julian calendar. Prior to that date, the
Julian calendar was commonly used. Different countries converted to the Gregorian calendar at
different times, and some not until early in the 20th century.
Although you might have heard of these two calendars, you might not have heard of the Julian
Day Count. This is similar in many ways to a UNIX time stamp. It is a count of the number of
days since a date around 4000 BC. In itself, it is not particularly useful, but it is useful for con-
verting between formats. To convert from one format to another, you first convert to a Julian
Day Count (JD) and then to the desired output calendar.
To use these functions, you will need to have compiled the calendar extension into PHP.
To give you a taste for these functions, consider the prototypes for the functions you would use
to convert from the Gregorian calendar to the Julian calendar:
int gregoriantojd (int month, int day, int year)
string jdtojulian(int julianday)
To convert a date, we would need to call both these functions:
$jd = gregoriantojd (9, 18, 1582);
echo jdtojulian($jd);
This echoes the Julian date in a mm/dd/yyyy format.
Variations of these functions exist for converting between the Gregorian, Julian, French, and
Jewish calendars and UNIX time stamps.
Managing the Date and Time
C
HAPTER 18
18
MANAGING THE
DATE AND TIME
399
23 7842 CH18 3/6/01 3:43 PM Page 399