[CLUE-Tech] PHP or MySQL date handling problem

Jed S. Baer thag at frii.com
Thu Jun 13 11:53:03 MDT 2002


Or maybe rant ;-)

I've run into a problem with PHP or MySQL (or both) with the handling of
dates. The problem is that neither of them provides a generalized
date/time parser that accepts a format string. MySQL provides the
DATE_FORMAT() function to convert a DATE type column to a string, using a
user-specifed format string, but not the converse. This means that I can't
easily write practical code to allow a preference for date-display format.

PHP, likewise, provides no similar function. The strtotime function works
with a variety of input strings, but again doesn't allow a user-supplied
format string.

To top it off, MySQL just converts invalid date/time strings to the "zero"
value of the appropriate type, and doesn't raise an error. So I have to
validate the date in PHP first, but again, without a proper date
conversion function, I have to write my own parser, and that seems a silly
thing to have to do.

I've looked around the usual PHP web sites, and haven't found anything
which really helps. Various user comments all center around using
conversions through Unix timestamps or some other parsing scheme, but all
of them presuppose a more limited range of formatting than that provided
by the MySQL DATE_FORMAT() function.

I guess I'll likely have to write my own 'pre-processor' in order to use
the date format I like, but that makes the ability to have a user-settable
date format in a preferences file much more difficult.

So, if anyone has any suggestions for a where I can find a "real", format
driven, date conversion function, I'd much appreciate it. Preferably for
PHP, as it's easier to add user-written functions to it, as opposed to
MySQL.

Later,
jed

-- 
We're frogs who are getting boiled in a pot full of single-character
morphemes, and we don't notice. - Larry Wall; Perl6, Apocalypse 5



More information about the clue-tech mailing list