## Sunday, March 27, 2011

### How to Calculate My Exact Age at Marriage Using PHP

Let's say my birth date was
1956-09-19 08:38:00 in Mesa, Arizona

Let's say my marriage date was
1981-08-14 11:38:00 in Las Vegas, Nevada

That puts my marriage three hours later in the day than my birth (albeit in a different time zone). So we say that the local time of my marriage is three hours later than the local time of my birth. Now, note that Arizona (Phoenix) has no daylight savings time, and that my marriage occurred in the summer, when Las Vegas (Los Angeles time) was on daylight savings time. Let's see how PHP handles that!

How do we calculate my exact age at marriage using PHP? I'll do it using the object-oriented method. We'll need two time zones (DateTimeZone objects) and two dates (DateTime objects), for a total of 4 objects.

// First we set up the time zone of my birth
\$my_birth_tz=new DateTimeZone('America/Phoenix');

// and we create my actual birth event.
\$my_birth=new DateTime('1966-09-19 08:38:00',\$my_birth_tz);

// Second we set up the time zone of my marriage
\$my_marriage_tz=new DateTimeZone('America/Los_Angeles');

// and we create my actual marriage event.
\$my_marriage=new DateTime('1991-08-14 11:38:00', \$my_marriage_tz);

// Third we create my age at marriage using the DateTime->diff method
\$my_marriage_age=\$my_marriage->diff(\$my_birth);

// And we echo the age.
echo \$my_marriage_age->format('%Y-%M-%D %H:%I:%S');
// Result: 24-10-25 03:00:00

PHP takes time zones and daylight savings time into account and reports my marriage age (at 11:38 compared to my 08:38 birth) as 3 hours more than a day. Hmm. Apparently Arizona and California are on the same time in the summer.

Let's try marrying me in the winter, when Las Vegas is off daylight savings time.

\$my_birth_tz=new DateTimeZone('America/Phoenix');
\$my_birth=new DateTime('1956-09-19 08:38:00',\$my_birth_tz);
\$my_marriage_tz=new DateTimeZone('America/Los_Angeles');
\$my_marriage=new DateTime('1981-12-14 11:38:00', \$my_marriage_tz);
\$my_marriage_age=\$my_marriage->diff(\$my_birth);
echo \$my_marriage_age->;format('%Y-%M-%D %H:%I:%S');
// Result: 25-02-25 04:00:00

This time PHP has no daylight savings time to account for, so it says Arizona and California are offset 1 hour, which is intuitive since they are neighboring time zones (America Mountain and America Central). I was one hour older than the clock indicated on the day of my winter marriage.

Note about the time diff format string:
Update: The documentaion for the DateInterval object formatting is located here:
http://www.php.net/manual/en/dateinterval.format.php
As far as I can tell, the datetime diff format string I ended up using is not documented anywere. The PHP manual says that the format string for the time diff is a "format accepted by date()". That would look like 'Y-m-d H:i:s', but that did not work. The other documented format I could find is the one accepted by strftime. That would look like '%Y-%M-%D %H:%i:%s', but that did not give leading zeroes for minutes and seconds.