Storing Dates in MySQL
DevShed have a new article on Date Arithmetic With MySQL, which acts as a kind of missing manual for MySQL’s powerful date arithmetic functions. It reminded me of something I’ve been meaning to write about for some time: my thoughts on storing dates in a PHP application that uses a MySQL backend
MySQL comes with a full featured API for handling dates, and several column types for use with the date and time functions. Unfortuantely, none of the available column types map directly to PHP’s internal format for handling dates, the Unix timestamp (an integer value recording the seconds since midnight on January 1st, 1970). I have seen many PHP applications where people have used a MySQL integer field to store date information as a Unix timestamp. While this works, it is inadvisable as it prevents you from using any of MySQLs powerful built in date handling abilities.
Instead, you can use MySQL’s UNIX_TIMESTAMP() and FROM_UNIXTIME() functions to convert Unix timestamps to MySQL date types as part of your SQL queries. This allows you to use MySQL’s internal date manipulation features without having to manually convert MySQL dates to PHP timestamps in your PHP applications. I usually use DATETIME fields to store timestamps, but the conversion functions work for any of MySQL’s date storage types.
Here’s a sample select query:
SELECT entries.*, UNIX_TIMESTAMP(added) as unixtime FROM enries ...
Assuming added is a DATETIME column, this adds an additional field to each returned row called ’unixtime’, containing an integer that can be passed straight to PHP’s handy date() function for formatting. Going the other way:
INSERT INTO entries SET title = 'The title', added = FROM_UNIXTIME(1057941242), ...
The second example is less useful, but at least demonstrates the function. Incidentally, when inserting things in to a MySQL database with the current time it’s generally a good idea to use MySQL’s NOW() function to set the time, like this:
INSERT INTO entries SET title = 'The title', added = NOW(), ...
Doing this increases consistency as it means that should you ever have a setup with multiple web servers talking to a single database server the database server’s time will be used as the standard, rather than potentially introducing errors from differing clocks on the server machines.
yes, my mind has always boggled at the number of comments submitted to the php manual with functions and tips for "parsing mysql's date format."
one thing to watch out for -- when using one of these types in a WHERE clause, it is best to do WHERE datecolumn = FROM_UNIXTIME(1057941242) and not WHERE UNIX_TIMESTAMP(datecolumn) = 1057941242. doing the latter won't take advantage of an index on that column.
jim winstead - 11th July 2003 19:03 - #
Regarding the use of NOW(), note that MySQL 4's query caching doesn't cache queries that contain that function (for good reason). To bypass this, I usually use something like $sql = 'UPDATE table SET added = "' . date('Y-m-d') . '" ...';
Adrian - 11th July 2003 19:13 - #
If the field is DATETIME, I have been able to format it any way with this line:
$formatdate = date("g:i A F j, Y",strtotime("$datetime"));Stephen - 11th July 2003 20:34 - #
Matt - 11th July 2003 21:58 - #
Mark Richards - 6th November 2004 22:16 - #
Dave Cone - 10th July 2005 00:50 - #
Dotan Cohen - 23rd August 2005 06:21 - #
Nathan P. Clarke - 3rd October 2005 22:40 - #
Martin - 11th October 2005 14:25 - #
Paul - 9th January 2006 23:37 - #
mani - 21st January 2006 09:26 - #
EE Jones - 9th February 2006 16:53 - #
Cherryaa - 5th April 2006 19:23 - #
David - 10th May 2006 18:56 - #
Neill - 17th May 2006 04:54 - #
h - 29th May 2006 12:36 - #
Hi! I am a newbie in PHP and only know the most basic things in SQL. I like this information on the use of MySQL for the time consistency. In fact, I use this method in my site, which I am currently developing in my computer. But when I uploaded my site and my database to a web host, it's surprising that times are no longer correct (3 hours advanced)! Why is this?
Note that every update or insert on my database regarding the use of time, I used NOW().
Here's how I retrieve these timestamp data from MySQL:
SELECT *, UNIX_TIMESTAMP(timelog) AS unixtime FROM _blog;
(result set retrieval code goes here)
echo date("m/d/Y H:i", unixgmt(" 8", $rs[$i]['unixtime']))
//unixgmt is a custom function I made that adds the number of secounds given the hour
//offset in the 1st parameter. In my case, GMT 8, hence the 8.
In my personal computer server the output date and time is correct but when I tried on webhost, it is 3 hours advanced. What is wrong?
Andrei - 1st June 2006 13:48 - #
Bandolmer - 3rd June 2006 12:02 - #
Andrei - 7th June 2006 20:20 - #
Marc Gear - 3rd July 2006 12:44 - #
zync oxide - 2nd August 2006 08:46 - #
Elliot - 16th August 2006 17:09 - #
Georgie - 21st August 2006 17:48 - #
Shane - 28th August 2006 15:06 - #
Steve - 1st September 2006 21:20 - #
AJ - 15th September 2006 00:55 - #
Russell Hutson - 20th September 2006 08:35 - #
Bas Hamar de la Brethoniere - 16th October 2006 19:32 - #