Microsoft Word - Sam's Teach Yourself MySQL in 21 Days - SAMS.doc

(singke) #1
+-----------------+

or, when used in a numeric context
mysql> SELECT SEC_TO_TIME(1000)+0;

which results in
+---------------------+
| SEC_TO_TIME(1000)+0 |
+---------------------+
| 1640 |
+---------------------+

Handling Unix Time


ƒ UNIX_TIMESTAMP([date])
ƒ FROM_UNIXTIME(UNIX_timestamp[,format])
The UNIX timestamp is the number of seconds since 1 January 1970. UNIX_TIMESTAMP(), without
the optional date parameter, returns the UNIX time of the present moment (UNIX_TIMESTAMP() will
return something like 949872354 ).


With the date specified in DATE, DATETIME, TIMESTAMP format, or a number in the form YYMMDD
or YYYYMMDD, it returns the Unix time of that date:


mysql> SELECT UNIX_TIMESTAMP('2020-10-04 22:23:00');


would result in


+---------------------------------------+


| UNIX_TIMESTAMP('2020-10-04 22:23:00') |


+---------------------------------------+


| 1601864580 |


+---------------------------------------+
FROM_UNIXTIME() has the opposite effect. When used with UNIX_timestamp as the sole parameter,
it returns the date and time in YYYY-MM-DD HH:MM:SS or YYYYMMDDHHMMSS format, depending
on whether it is used in a string or numeric context. For example


mysql> SELECT FROM_UNIXTIME(949872354)*1;


would result in date and time information in numeric format:


+-----------------------------+


| FROM_UNIXTIME(949872354)*1 |


+-----------------------------+


| 20000206212554 |


+-----------------------------+
You can optionally give this function a format parameter, which will cause the output to be formatted
according to the rules of the FORMAT_DATE() function. For example, you can work out the day on
which the billionth second of UNIX time will fall:


mysql> SELECT FROM_UNIXTIME(1000000000, '%W, %M');


+-------------------------------------+


| FROM_UNIXTIME(1000000000, '%W, %M') |


+-------------------------------------+


| Saturday, September |


+-------------------------------------+

Free download pdf