DATE_FORMAT, FROM_UNIXTIME, UNIX_TIME

Source: Internet
Author: User
DATE_FORMAT, FROM_UNIXTIME, UNIX_TIME

  1. DATE_FORMAT (NOW (), '% B % d % Y % h: % I % p ')
  2. DATE_FORMAT (NOW (), '% m-% d-% Y ')
  3. DATE_FORMAT (NOW (), '% d % B % Y ')
  4. DATE_FORMAT (NOW (), '% d % B % Y % T: % f ')

Results: Dec 29 2008 16:25:46 PM 12-29-2008 29 Dec 08 29 Dec 2008

2. MySQL database date and time functions FROM_UNIXTIME (), UNIX_TIME ()... instance: date => int (11)

  1. SELECT FROM_UNIXTIME (date, '% Y-% c-% d % h: % I: % S') as post_date,
  2. Date_format (NOW (), '% Y-% c-% d % h: % I: % S') as post_date_gmt
  3. FROM 'article' where outkey = 'y'

1. FROM_UNIXTIME (unix_timestamp) parameter: usually a ten-digit number, such as: 1344887103 return value: There are two, may be similar to 'yyyy-MM-DD HH: MM: A string like 'SS 'may also be similar to YYYYMMDDHHMMSS. A number such as uuuuuu. the specific returned result depends on the form of the function being called.

  1. Mysql> select FROM_UNIXTIME (1344887103 );
  2. + --------------------------- +
  3. | FROM_UNIXTIME (1344887103) |
  4. + --------------------------- +
  5. | 03:45:03 |
  6. + --------------------------- +
  7. 1 row in set (0.00 sec)

2. FROM_UNIXTIME (unix_timestamp, format) parameter unix_timestamp: same as the parameter meaning in the method FROM_UNIXTIME (unix_timestamp); Parameter format: format of the time string displayed after conversion; return value: A string displayed in the specified time format;

  1. Mysql> select FROM_UNIXTIME (1344887103, '% Y-% M-% D % h: % I: % S ');
  2. + ----------------------------------------------- +
  3. | FROM_UNIXTIME (1344887103, '% Y-% M-% D % h: % I: % S') |
  4. + ----------------------------------------------- +
  5. | 2012-August-14th 03:45:03 |
  6. + ----------------------------------------------- +
  7. 1 row in set (0.00 sec)
  8. Mysql> select FROM_UNIXTIME (1344887103, '% Y-% m-% D % h: % I: % S ');
  9. + ----------------------------------------------- +
  10. | FROM_UNIXTIME (1344887103, '% Y-% m-% D % h: % I: % S') |
  11. + ----------------------------------------------- +
  12. | 03:45:03 |
  13. + ----------------------------------------------- +
  14. 1 row in set (0.00 sec)

1. return value of UNIX_TIMESTAMP (): The UNIX-Format numeric string of the current time, or the UNIX timestamp (the number of seconds since UTC time '2017-01-01 00:00:00 ), usually 10, such as 1344887103.

  1. Mysql> select unix_timestamp ();
  2. + ------------------ +
  3. | Unix_timestamp () |
  4. + ------------------ +
  5. | 1, 1344887103 |
  6. + ------------------ +
  7. 1 row in set (0.00 sec)

2. UNIX_TIMESTAMP (date) parameter: date may be a DATE string, DATETIME string, TIMESTAPE string, or a numeric string similar to YYMMDD or YYYYMMDD. Return the number of seconds from UTC time '2017-01-01 00:00:00 'to this parameter. The server interprets the date parameter as a value of the current time zone and converts it to an internal time in UTC format. The client can set the current time zone on its own. When UNIX_TIMESTAMP () is used for a TIMESTAMP column, the function directly returns the internal TIMESTAMP value. if you pass a time out of the range to UNIX_TIMESTAMP (), the return value is zero.

  1. Mysql> SELECT UNIX_TIMESTAMP ();
  2. + ------------------ +
  3. | UNIX_TIMESTAMP () |
  4. + ------------------ +
  5. | 1, 1344888895 |
  6. + ------------------ +
  7. 1 row in set (0.00 sec)
  8. Mysql> SELECT UNIX_TIMESTAMP ('2017-08-14 16:19:23 ');
  9. + --------------------------------------- +
  10. | UNIX_TIMESTAMP ('2017-08-14 16:19:23 ') |
  11. + --------------------------------------- +
  12. | 1, 1344932363 |
  13. + --------------------------------------- +
  14. 1 row in set (0.00 sec)

Note: If you use UNIX_TIMESTAMP () and FROM_UNIXTIME () to convert the TIMESTAMP value to the Unix TIMESTAMP value, the precision will be lost because the ING is not one-to-one in both directions. For example, due to changes in the local time zone, two UNIX_TIMESTAMP () values may be mapped to the same Unix timestamp value. FROM_UNIXTIME () is only mapped to the original timestamp value. Here is an example where TIMESTAMP is used in the CET time zone:

  1. Mysql> SELECT UNIX_TIMESTAMP ('2017-03-27 03:00:00 ');
  2. + --------------------------------------- +
  3. | UNIX_TIMESTAMP ('2017-03-27 03:00:00 ') |
  4. + --------------------------------------- +
  5. | 1, 1111885200 |
  6. + --------------------------------------- +
  7. Mysql> SELECT UNIX_TIMESTAMP ('2017-03-27 02:00:00 ');
  8. + --------------------------------------- +
  9. | UNIX_TIMESTAMP ('2017-03-27 02:00:00 ') |
  10. + --------------------------------------- +
  11. | 1, 1111885200 |
  12. + --------------------------------------- +
  13. Mysql> SELECT FROM_UNIXTIME (1111885200 );
  14. + --------------------------- +
  15. | FROM_UNIXTIME (1111885200) |
  16. + --------------------------- +
  17. | 03:00:00 |
  18. + --------------------------- +

Reference: https://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.