views:

601

answers:

5

I want my product, like every other web product out there, to pull a data information from my database and convert it to the user's timezone before displaying it on a page. I am using PHP and MySQL.

Is the use of TIMESTAMP fields more common than the use of DATETIME fields?

From my research, it seems that using DATETIME fields, I would have to

  1. Store all date information in UTC
  2. call date_default_timezone_set('UTC') each time the application starts
  3. manually subtract the user's UTC offset hours from dates created using date() being based on the current date/time
  4. manually subtract the user's UTC offset hours from existing dates passed to and formatted by date()

Using TIMESTAMPS for my date fields, it seems I will only have to run the following when a database connection is made: SET time_zone='the users timezone' (eg. America/Los_Angelos).

Considering this, I would deduce most people would be inclined to using TIMESTAMPs rather than DATETIMEs...is this accurate?

+1  A: 

From this location:

The SQL Server timestamp data type has nothing to do with times or dates. SQL Server timestamps are binary numbers that indicate the relative sequence in which data modifications took place in a database. The timestamp data type was originally implemented to support the SQL Server recovery algorithms.

Edit: to answer your question, DATETIME is more commonly used, as TIMESTAMP is not intended to be used for storing general Date / Time information.

McWafflestix
Thank you for the information, but this does not answer my question. Which is most commonly used?
Chad Johnson
Hey, thanks again for the response. I've read the MySQL TIMESTAMP documentation, and I see nothing supporting the statement "TIMESTAMP is not intended to be used for storing general Date / Time information." Can you provide a source?
Chad Johnson
Yes, the source is linked to in the above answer; the word "this" in my answer is a hyperlink to the source of the information.
McWafflestix
Ah! I missed that :) Thanks.
Chad Johnson
SQL Server != MySQL ... this does not apply.
razzed
A: 

TIMESTAMP is special in that it can be autoupdated on created or row change. See the timestamp docs.

Basically, unless you want automagic behaviour on the part of MySQL, use DATETIME. DATETIME can also represent a larger range.

Do you know whether DATETIME or TIMESTAMP is more commonly used?
Chad Johnson
In the case I described, wouldn't it make a lot of sense to go with TIMESTAMP over DATETIME?
Chad Johnson
You can disable the automatic updating behavior of TIMESTAMPs in MySQL by adding "DEFAULT 0" to a column definition, but you get the perks of automatic time zone translation.
razzed
+1  A: 

The TIMESTAMP field is commonly used to support concurrency checking. When a table record is being modified by a user, through a form or some other mechanism, the database knows that it is updating the same record that the user just read because the TIMESTAMP that the user is submitting with his record changes still matches the TIMESTAMP in the database record.

If another user modifies the record prior to the first user performing a save, the TIMESTAMP that the first user read would no longer match the TIMESTAMP that is in the database. What happens next depends on how the system handles concurrency violations. Some systems will prevent the save. Other systems will ask the first user if they want to overwrite the other person's changes.

Robert Harvey
Could I have an explanation for the downvote, please? Thanks.
Robert Harvey
I would argue that "commonly used to support concurrency checking" to be, well, misleading and against my own experience. TIMESTAMPs are used to mark a database record as being modified (or created, or updated, depending on how it's defined). An *application* of TIMESTAMPs is concurrency checking when you have multiple users editing the same record. However, implementation of concurrency checking and TIMESTAMPs (in a database) is way beyond the definition of what a TIMESTAMP does. So, in short, downvote. Unless, of course, there is some built-in MySQL feature I'm unaware of.
razzed
Splitting hairs, I think.
Robert Harvey
Hm. Well, the question is about displaying dates in the current time zone, and your answer describes about how timestamps can be used for concurrency checking. Doesn't seem related to me. Sorry if you disagree.
razzed
+2  A: 

No, I don't think that's accurate.

Generally, you use datetime to store just that - the date and time that something is happening, when it's entered into your site. You use timestamp to keep track of when data was entered into your site for your program's use. timestamp shouldn't have to keep track of time zones because all of the work happens in your db server's time zone - whereas datetime has more options for presenting it properly for it to make sense as output to the user.

So, for the question "Which is more common for presenting user-timezone data", I'd say the answer is DATETIME.

John Fiala
I'm just not seeing the advantage of doing this each time I want to display date information, when I could have the database manager do all the work for me: strtotime($utcTimestamp) + $utcOffset * 60 * 60. I would have to do this literally hundreds of places throughout my code base. Yes I could use a wrapper function to do the math, but I would still have to add hundreds of calls to this function. In light of this, is it still really sensible to use DATETIME rather than TIMESTAMP?
Chad Johnson
+1  A: 

If you need to do time zone manipulation, and you don't need to store dates before 1970 or after 2038, use TIMESTAMPs.

You can do a single SQL statement:

set timezone='America/New_York';

And all dates retrieved from the database will be in EDT.

I would avoid doing UTC offset math as it becomes difficult to handle all of the cases correctly. Your best bet is to set the PHP and MySQL timezones appropriately for each user.

TIMESTAMPs store dates ONLY as UTC, and MySQL handles the translation for it.

When storing dates:

set time_zone='America/New_York';
insert into SomeTable ( Name, Created, Modified ) VALUES ( 'Dude', '2009-06-15 11:54:00', '2009-06-15 11:54:00');
set time_zone='GMT';
SELECT * FROM SomeTable ORDER BY ID DESC LIMIT 1;
+----+------+---------------------+---------------------+
| ID | Name | Created             | Modified            |
+----+------+---------------------+---------------------+
|  1 | Dude | 2009-06-15 15:54:00 | 2009-06-15 15:54:00 | 
+----+------+---------------------+---------------------+

Just set the database time zone to the zone the user is in, and it will correctly "covert" it to the right GMT date.

Just make sure you do not "double convert" dates in both PHP and MySQL.

razzed
Not being able to store dates before 1970 or after 2038 seems like a fatal disadvantage, don't you think? Is there any kind of workaround?
Chad Johnson
Sure, use DATETIME. I use timestamps for a statistics program which I work on, so 2038 is not an issue. It really depends on the application so make the decision which makes the most sense for your application. In general, when displaying dates/times for timezones, it's typically information which is relatively current, such as schedule information, etc. I can't imagine an application which needs dates before 1970 which needs to translate timezones which can't use other methods, such as MySQL "CONVERT_TZ":http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz
razzed