I have thousands of dates in the following format:
2011-10-02T23:25:42Z
(aka ISO 8601 in UTC)
What MySQL data type should I use for storing such a ISO8601 date in a MySQL database? E.g. Datetime
, timestamp
or something else?
Which is best for comparison (eg. getting records between two dates/times) and ordering the results from queries? What about if the database is very large?
And what would be the best way to convert the above PHP string for MySQL storage? (I'm guessing date_default_timezone_set('UTC');
would be used?)
You can easily convert the date using
strtotime
function ofphp
:Now you would simply insert your date in
MySQL
usingtimestamp
ordatetime
depending on which one fit the most your needs. Here the most important things you should know about both types.Timestamp
on update current_timestamp
on columns for all versions.NULL
is not a possible default valueUTC
for storage, and converted back fromUTC
to the current time-zone for retrieval.Datetime
5.6.5
According to the previous points I stated, then you should use
timestamp
for a very large database as the storage is smaller, and the index faster which will give you better performance for comparison. However, you MUST MAKE SURE your date will fit the limits of thetimestamp
I previously mentioned, else you have no choice and must usedatetime
.Documentation for
strtotime
: http://php.net/manual/en/function.strtotime.phpAnd please, for the sake of SO's answerer who keep repeating every day to not use the
mysql*
DEPRECATED functions, please usePDO
ormysqli*
when you will do your inserts.http://php.net/manual/en/book.pdo.php
http://php.net/manual/en/book.mysqli.php
I think that keeping your date-time values in field of type
DATETIME
would be kind of natural way.From my own experience with my current PHP application, only
read
/write
operations concerning this information may be problematic.One of possible solutions (assuming that you use
DATETIME
data type) for properly performing the whole process could be the following approach:Reading DATETIME values for PHP use
DATETIME
fields from your database converting them in the query to string representation in the form of'2011-10-02T23:25:42Z'
by usingDATE_FORMAT
MySQL function with'%Y-%m-%dT%H:%i:%sZ'
formatting string (docs on DATE_FORMAT)DateTime
class objects andDateTime::createFromFormat
static method given'Y-m-d\TH:i:s\Z'
formatting string (T
andZ
are escaped to avoid treating them as formatting directives) (docs for the method).Writing PHP date-time to MySQL database
DateTime
class object to our ISO 8601 in UTC format string representation usingDateTime
class object'sformat
method with the same as before'Y-m-d\TH:i:s\Z'
formatting string (documentation).INSERT
/UPDATE
operation on database information using such prepared string as a parameter for MySQL functionSTR_TO_DATE
(with'%Y-%m-%dT%H:%i:%sZ'
formatting string) which converts it to real databaseDATETIME
value (docs on STR_TO_DATE).Example code in PHP
Below please find a draft example of such approach using PDO objects:
This approach helped me a lot in operating date-time values between PHP and MySQL database.
I hope it might occur helpful for you also.
Here are the points why it is better to use datetime.
You can use
DateTime
data type for storing the date and time.Use
CAST
function to cast such strings into mysqlDateTime
type.Here is an example:
This will give you
2011-10-02 23:25:42
.Hope this will help you.
You can not store date in raw UTC ISO8601 format (with
2011-10-02T23:25:42Z
representation) and save all SQL DATETIME functionality.But you should know, that MySQL ( regarding to http://dev.mysql.com/doc/refman/5.5/en/datetime.html ) always store time/date in UTC. Also you can modify timezone for your connection http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
So, if you execute in PHP
and in MySQL
sure PHP and MySQL would use UTC.
After that you can convert all database strings to DateTime without caring about timezone mismatch.
To convert any PHP DateTime (without carrying about its internal timezone) to MySQL datetime string you should set DateTime object timezone to UTC.