Should I be storing a Delphi TDateTIme, or perhaps convert to Unix timestamp first? Or mayeb eeven as a string?
How should I declare the column in MySql? As a Double, or DateTime (or Integer if I use Unix timestamp)?
Whta is “correct”, or what is easiest if I want to be able to display a string with “yyyy mm dd hh:mm:ss” (or similar) and also to be able to get an elapsed time from comparing two values?
Btw, the program will only ever be used in one tiemzone – which does not have daylight savings time.
I am confused and can’t seem to find this discussed anywhere. Any helpful URLs?
Typically, you should do neither: the data types in the database layer ought to be meaningful (as possible) on their own and not depend on your application to interpret them. As @Jim DeLaHunt says, this enables the database to easily manipulate/interpret them from SQL as required (and also enables you to easily access the same data from another application codebase in the future).
MySQL has five temporal types, only two of which store both a date and a time:
DATETIMEandTIMESTAMP.As others have alluded, the difference comes down to whether you wish to store the timezone – although I find that quite a confusing way of looking at it:
TIMESTAMPuses the session’stime_zonevariable to convert input into a UTC timestamp and then back again for output: it’s useful for specifying an exact moment in time;DATETIMEsimply stores the date and time without regard to timezone, much like taking a photograph of a calendar and clock: it’s useful for specifying an event that occurs in the same local time globally.Just as you would declare any other column, you specify the relevant data type after the column name.
Beware that
TIMESTAMPhas additional features, such as automatic update, which you may wish to disable in your column declaration if so desired.Using one of the above temporal types, you will be able to do all of this (using date functions as required). The default output of
TIMESTAMPandDATETIMEtypes is a string in'YYYY-MM-DD HH:MM:SS'format.In particular, the “elapsed time” from comparing two values could for example be obtained with MySQL’s
TIMEDIFF()function: