Update: Imported on 6 Oct 08 from http://kitt.hodsden.com/
For MySQL 5+
mysql> SELECT FROM_UNIXTIME(created) FROM node WHERE nid = 10;
+------------------------+
| from_unixtime(created) |
+------------------------+
| 2005-05-22 10:22:04 |
+------------------------+
For MySQL 4.1 and lower:
Having
converted MySQL TIMESTAMPs to Drupal's int(11) style timestamps, which records the number of seconds since epoch (midnight, January 1st 1970), I also had cause to convert the other direction: from an int(11) to a MySQL TIMESTAMP.
MySQL provides a utility function to convert from unix timestamps (the int(11) format):
FROM_UNIXTIME
.
From the
MySQL documentation:
FROM_UNIXTIME(unix_timestamp
)
, FROM_UNIXTIME(unix_timestamp
,format
)
Returns a representation of the unix_timestamp
argument as a value in
'YYYY-MM-DD HH:MM:SS'
or YYYYMMDDHHMMSS
format, depending on
whether the function is used in a string or numeric context.
mysql> SELECT FROM_UNIXTIME(875996580);
-> '1997-10-04 22:23:00'
mysql> SELECT FROM_UNIXTIME(875996580) + 0;
-> 19971004222300
If format
is given, the result is formatted according to the
format
string. format
may contain the same specifiers as
those listed in the entry for the DATE_FORMAT()
function.
mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),
-> '%Y %D %M %h:%i:%s %x');
-> '2003 6th August 06:22:58 2003'
Okay, so
FROM_UNIXTIME
can format the time/date in pretty much any way you'd like it, with the optional format argument.
Since we want the
TIMESTAMP
format, force MySQL to treat the date as an integer by adding 0 to it:
mysql> SELECT FROM_UNIXTIME(creation_date) + 0 FROM paid_support LIMIT 1;
+----------------------------------+
| FROM_UNIXTIME(creation_date) + 0 |
+----------------------------------+
| 20031102084514 |
+----------------------------------+
1 row in set (0.00 sec)