Tutorial :MySQL date or PHP time?


I usually store dates as integers using PHP's time() function in a MySQL database rather than using MySQL's date format simply because it's easier to manipulate when I pull it back out, but are there any disadvantages to this?



There's always the obvious disadvantage: The range that you can store is limited från 1970 to 2038. If you need to store dates outside of this range, you'll generally need to use another format. The most common case I've found where this apply is to birthdates.


I think that the most important reason that people chose to use one of the built-in date-types it that the data is easier to interpret. You can do a simple select, and understand the values without having to format the response further.


A good technical reason to use the date types is that it allows for indexed query in some cases that unix timestamps doesn't. Consider the following query:

SELECT * FROM tbl WHERE year(mydate_field) = 2009;  

If mydate_field is of a native date type, and there's an index on the field, this query will actually use an index, despite the function call. This is pretty much the only time that mysql can optimize function calls on fields like this. The corresponding query on a timestamp field won't be able to use indices:

SELECT * FROM tbl WHERE year(from_unixtime(mytimestamp_field)) = 2009;  

If you think about it for a bit, there's a way around it, though. This query does the same thing, and will be able to use index optimizations:

SELECT * FROM tbl WHERE mytimestamp_field > unix_timestamp("2009-01-01") AND mytimestamp_field < unix_timestamp("2010-01-01");  


Generally, I store dates as unix time, despite the disadvantages. This isn't really based on it's merits, but rather it's because I'm used to it. I've found that this simplifies some calculations, but complicate others. For example, it's very hard to add a month to a unix timestamp since the number of seconds per month varies. This is very easy using the mysql DATE_ADD() function. However, I think that in most cases it actually simplifies calculations. For example, it's quite common that you want to select the posts from, say, the last two days. If the field contains a unix timestamp this can be done easily by simply doing:

SELECT * FROM tbl WHERE mytimestamp_field > time() - 2*24*3600;  

It's probably a matter of taste, but I personally find this faster and easier than having to rember the syntax of a function such as DATE_SUB().


Unix timestamps can't store time zone data. I live in sweden which has a single timezone, so this isn't really a problem for me. However, it can be a major pain if you live in a country that spans multiple timezones.


One disadvantage is that you won't be able to manipulate and query those dates using SQL functions.


I used to do the same, but now I store it as a MySQL DateTime - simply because that means when looking at the raw data in the database I can interpret it easily.

Other than that, it's possibly easier to work with the data with other languages that don't use the UNIX timestamp so heavily (as PHP does), but there isn't really a huge pull either way.


UNIX timestamp has obvious limitations as to the range of dates that you're able to store.

I also always use DATETIME fields now. You can do a lot of DATE math using SQL so you can pull out useful info like DATEDIFF between now and a stored date without using any PHP at all.


You can define an auto-update clause for MySQL's timestamps in your table definition.


There are many disadvantages:

  • Lack of precision; Unix time is only accurate to the second, and only for dates between 1901-12-13 and 2038-01-19 when using the typical 32-bit integer
  • You can't use any built-in database functions to query or manipulate the data
  • You can't store a timezone

If you need a time_t, it's easy enough to convert to one in code.


Only a couple I can think of:
* If another non-php application needs to use the database, this will be in a difficult format to read.
* If you want to do any SQL based work on these dates (e.g. adding a month or getting all values for a particular year, etc), this will be more difficult.


A slight loss of detail. The MySQL Datetime variable can be very precise.

Also, if you're gonna have to compare dates in your database, the date format has some built in functions you won't be able to use.


I think that for scalability reasons it is better to use Unix time-stamps.


  • Always stored in UTC timezone(if you have servers across multiple time-zones no conversion is needed).
  • Applications convert them to the preferred timezone(This occurs only once, at last level possible).
  • Not strings(those are huge in comparison with integers).
  • Less database calculations(Stuff like created < 19345345345-24*60*60 is calculated once).

EDIT: MySQL Timestamps are not stored internally as strings, but when pulled out of the database those are converted to strings. DATETIME type is not modified by MySQL, meaning that if you put a date in the database you get the same.

If you have visitors on a website from a different timezone you would have to convert dates like string->string instead of integer->string). In some countries dates are not just numbers(For example in France it is Mardi 15 mai 2012 I prefer doing that in PHP or JS. I think that a simpe convertion integer->string is faster than Integer->String->String. Plus no headache if migrating to servers in other country.

Fake disadvantages:

  • I believe that the limited range of Unix time-stamps isn't actually limited. A time-stamp is an integer in the database, so you can adjust the size. By default an unsigned integer is int(10) meaning you can store numbers up to 4294967295, but it's limits are not fixed so we can easily change int(10) int to int(16) bigint


That's not too bad but you'll be loosing some built in functionality such as:

select * from table1 where dateColumn = getDate()-30

Use datetime if you can!

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »