#native_company# #native_desc#

On User-Defined Timezones in PHP Page 2

By David Whittington
on September 11, 2003

Why use Timestamps?
When we initially designed our portal, the choice was made to store time values
in the database as timestamps. This means that as soon as a user enters a date,
it is converted from a text string to a timestamp for internal manipulation and
storage, and only converted back to human-readable form when being displayed
back to the user. Using timestamps it is possible to easily manipulate the date
with a combination of simple math and the powerful functions provided by PHP
(‘date’ and ‘mktime’).
For example, finding midnight of any given day is a simple matter of
applying this equation: $timestamp - ($timestamp % (24*60*60)).
It is also handy to use the very flexible ‘mktime’ function. It allows the
programmer to do strange things like input “January -1, 2000” and expect a
timestamp representing “December 31, 1999.” The ‘date’ function is also very
powerful and allows for formatting of a date string from any timestamp. But,
lurking behind their seemingly benign facade is a maelstrom of debugging

The Inadequacy of PHP Time Functions

PHP’s ‘mktime’ and ‘date’ functions work well as a pair without the help of any
other timestamp manipulation routines, but only if the application in which
they are used is concerned solely with display and entry of time in the servers
timezone. If an application needs to handle entry from a timezone other than
that in which the server is located something more than ‘mktime’ and ‘date’ is
Two things are required to accomplish this: a location independent format for
storing time in the database, and methods to translate to and from that
format into the user’s local time. Simply deciding to store all timestamps in
the database as seconds since the epoch (12:00AM January 1st 1970) in GMT is
sufficient for determining the location independent time format. However,
writing methods to translate to and from that format is a bit more difficult.
It is not a simple task to translate a value like “2:00AM August 18th 1982”
into a single integer. There are all kinds of nasty issues involving leap years,
daylight savings, and such that must be dealt with to get the timestamp value.
Fortunately as we already mentioned PHP has a nice set of functions to do all
that for you. However a very specific knowledge of what those functions do is
necessary in order to use them to convert the timestamps they return into a
location independent format.
Unfortunately to many people the PHP documentation will not be very helpful on
this point. Although to an enlightened few who are already well versed in the
wonders of *nix timestamps it may be clear from reading it what ‘mktime’
actually returns; for most of us the answer is not so obvious.