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?
views:
504answers:
9I 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.
One disadvantage is that you won't be able to manipulate and query those dates using SQL functions.
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!
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.
Range:
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.
Readability:
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.
Indexes:
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");
Calculations:
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().
Timezones:
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.
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.
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.
You can define an auto-update clause for MySQL's timestamps in your table definition.
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html