Re: Formatting timestamp dates retrieved from MySQL

From: Date: Wed, 11 Oct 2000 17:28:07 +0000
Subject: Re: Formatting timestamp dates retrieved from MySQL
References: 1 2 3  Groups: php.db 
Request: Send a blank email to php-db+get-3528@lists.php.net to get a copy of this message
> Date formatting has always been ugly, and the MySQL timestamp returns a yyyymmdd, > rather than the mmddyyyy used by the PHP date formatting functions. Is there an > alternative to this, rather ugly, extraction and formatting to get yyyy-mm-dd > rather than a tight yyyymmdd? I know I could do the formatting in the "select", You could just write a wrapper function to do the appropriate substring switching. Something like function iso2us_date ($input_date) { yyyymmdd 01234567 return substr($input_date, 4, 2) . substr($input_date, 6, 2) . substr($input_date, 0, 4); } would work. However, I'd strongly advise that you reconsider SELECT *. It's much, much cleaner to list the fields. It's faster, easier to maintain and allows you to do things like format dates however you'd like. I find that the cleanest approach is to have MySQL return unix timestamps for all dates and then use a function to format them for display, which means all of your date manipulation code is completely format-independent (and a little faster) and makes it very easy to change how dates are displayed site-wide (I typically have a display_short_date (e.g. MM-DD-YY), display_long_date (YYYY-MM-DD HH:MM:SS), etc).

« previous php.db (#3528) next »