« Search PHP & MySQL documentation from Firefox 2 search box | IE7 and Firefox2 search providers for Flash & Coldfusion LiveDocs »

Dates in PHP and MySQL

3rd November 2006

I see a lot of people on forums and on my training courses asking about the best way (or any way) to manage dates stored in a MySQL database and used in PHP. Three options follow, but first the problem.

PHP uses unix timestamps for all its date functionality. It has methods to convert these timestamps into pretty much any text format you could want but internally it uses the timestamp format. A timestamp is simply an unsigned integer. Specifically, it’s the number of seconds that have elapsed since midnight on January 1st 1970 (greenwich mean time).

MySQL has three date types for use in columns. These are DATETIME, DATE, and TIMESTAMP. DATETIME columns store date and time as a string in the form YYYY-MM-DD HH:MM:SS (e.g. 2006-12-25 13:43:15). DATE columns use just the date part of this format - YYYY-MM-DD (e.g. 2006-12-25). TIMESTAMP columns, despite their name, are nothing like the unix timestamps used in PHP. A TIMESTAMP column is simply a DATETIME column that automatically updates to the current time every time the contents of that record are altered. (That’s a simplification but broadly true and the details are not important here). In particular, since version 4.1 of MySQL the TIMESTAMP format is exactly the same as the DATETIME format.

So the problem is how to work with these two very different date formats - the PHP timestamp integer and the MySQL DATETIME string. There’s three common solutions…

  1. One common solution is to store the dates in DATETIME fields and use PHPs date() and strtotime() functions to convert between PHP timestamps and MySQL DATETIMEs. The methods would be used as follows -

    $mysqldate = date( 'Y-m-d H:i:s', $phpdate );
    $phpdate = strtotime( $mysqldate );
  2. Our second option is to let MySQL do the work. MySQL has functions we can use to convert the data at the point where we access the database. UNIX_TIMESTAMP will convert from DATETIME to PHP timestamp and FROM_UNIXTIME will convert from PHP timestamp to DATETIME. The methods are used within the SQL query. So we insert and update dates using queries like this -

    $query = "UPDATE table SET
        datetimefield = FROM_UNIXTIME($phpdate)
        WHERE...";
    $query = "SELECT UNIX_TIMESTAMP(datetimefield)
        FROM table WHERE...";
  3. Our last option is simply to use the PHP timestamp format everywhere. Since a PHP timestamp is an unsigned integer, use an unsigned integer field in MySQL to store the timestamp in. This way there’s no conversion and we can just move PHP timestamps into and out of the database without any issues at all.

    Be aware, however, that by using a unsigned integer field to store your dates you loose a lot of functionality within MySQL because MySQL doesn’t know that your dates are dates. You can still sort records on your date fields since php timestamps increase regularly over time, but if you want to use any of MySQL’s date and time functions on the data then you’ll need to use FROM_UNIXTIME to get a MySQL DATETIME for the function to work on.

    However, if you’re just using the database to store the date information and any manipulation of it will take place in PHP then there’s no problems.

So finally we come to the choice of which to use. For me, if you don’t need to manipulate the dates within MySQL then there’s no contest and the last option is the best. It’s simple to use and is the most efficient in terms of storage space in the data table and speed of execution when reading and writing the data.

However, some queries will be more complicated because your date is not in a date field (e.g. select all users who’s birthday is today) and you may lose out in the long run. If this is the case it may be better to use either option 1 or 2. Which of these you use depends on whether you’d rather place the work on MySQL or PHP. I tend to use option 2 but there’s no right or wrong answer - take your pick.

So to summarise, for those who’ve skipped straight to the last paragraph, most of the time I use option 3 but occasionally I use option 2 because I need MySQL to know the field contains a date.

Read more articles about PHP, MySQL 

10 Comments

  • This is really a great article. Nicely stated, lots of imformation given clearly. Thanks!

    Greg  |  6th December 2006 at 5:53 pm

  • Good article. I am also trying to decide what to use and what not to.

    Sohail Abid  |  8th December 2006 at 3:05 pm

  • Useful article
    Many Thanks

    But is there any function in php which can convert number of seconds back to datetime format

    Atif  |  2nd January 2007 at 5:02 pm

  • If by number of seconds you mean a unix timestamp, then just use the date() function - date( 'Y-m-d H:i:s', $timestamp )

    richard  |  2nd January 2007 at 5:18 pm

  • One thing to bear in mind when using UNIX timestamps is that they always start at 1/1/1970. Some UNIX/MySQL implementations (and my Fedora 6 seems to be one of them) cannot store pre-1970 dates, and these always come out as 0 (rather than a negative integer). For this reason, I have moved towards the MySQL DATE type and do the string conversion manually.

    Dan Walker  |  6th January 2007 at 1:12 pm

  • Thanks Dan. That’s something I should have mentioned above.

    richard  |  6th January 2007 at 4:00 pm

  • Very cool article. I think a nice addition would be getting dates from mysql and displaying them using php formatting functions. I’ve pretty much got it figured out. But you may be able to explain it better than a lot of the other resources I’ve found for displaying dates in a user-friendly format. Thanks!

    Joey  |  19th March 2007 at 2:35 am

  • man……….u dont know how helpful u have been…..

    i was fxxxxxg frustrated due to confusion of timestamp……….was abt to break my laptop……but then i found ur article…….and the problem was solved in a min…….thx buddy…

    Babu  |  4th May 2007 at 3:28 am

  • Very well written article! clear and simple! Thanks for publishing!

    Leandro  |  9th May 2007 at 5:44 pm

  • I just wanted to say, thanks for a great article, has helped me heaps!

    Nick  |  30th May 2007 at 10:00 am

Comments closed.