Home >Database >Mysql Tutorial >What are the mysql date format conversion functions?
mysql date format conversion functions are: 1. [SEC_TO_TIME(seconds)] converts seconds into time; 2. [TIME_TO_SEC(time)] converts time into seconds; 3. [ADDTIME(time, times)] Add times to time.
[Related learning recommendations: mysql tutorial (video)]
mysql date format conversion functions are:
1. Get the current system date
SELECT CURDATE() SELECT CURRENT_DATE()
-> 2016-01-16 -> 2016-01-16
2. Get the current system time
SELECT CURTIME() SELECT CURRENT_TIME()
-> 17:44:22 -> 17:44:22
3. NOW(), SYSDATE(), CURRENT_TIMESTAMP(), LOCALTIME(): Get the current date and time of the system
SELECT NOW() SELECT SYSDATE() SELECT CURRENT_TIMESTAMP() SELECT CURRENT_TIMESTAMP SELECT LOCALTIME() SELECT LOCALTIME
-> 2016-01-16 17:44:41 -> 2016-01-16 17:44:41 -> 2016-01-16 17:44:41 -> 2016-01-16 17:44:41 -> 2016-01-16 17:44:41 -> 2016-01-16 17:44:41
4, UNIX_TIMESTAMP (date): Get the timestamp
SELECT UNIX_TIMESTAMP() SELECT UNIX_TIMESTAMP(‘2016-01-16') SELECT UNIX_TIMESTAMP(‘2016-01-16 23:59:59')
-> 1452937627 -> 1452873600 -> 1452959999
5, FROM_UNIXTIME( unix_timestamp,format): Convert timestamp into date and time
SELECT FROM_UNIXTIME(1452959999) SELECT FROM_UNIXTIME(1452959999,'%Y-%m-%d %H:%i:%s')
-> 2016-01-16 23:59:59 -> 2016-01-16 23:59:59
6, SEC_TO_TIME(seconds): Convert seconds into time
SELECT SEC_TO_TIME(2378)
-> 00:39:38
7, TIME_TO_SEC(time): Convert time into Seconds
SELECT TIME_TO_SEC(‘22:23:00')
-> 2378
8. ADDTIME(time, times): Add times to time
SELECT ADDTIME(“2015-12-31 23:59:59”,'01:01:01')
-> 2016-01-01 01:01:00
9. CONVERT_TZ(date,from_tz,to_tz): Convert time zone
SELECT CONVERT_TZ(‘2004-01-01 12:00:00','+00:00','+10:00')
-> 2004-01-01 22:00:00
10. STR_TO_DATE(date, format): Convert a string into a date and time in format format
SELECT STR_TO_DATE(‘2015-01-01', ‘%Y-%m-%d')
-> 2015-01-01
11. LAST_DAY(date): Get the date of the last day of the month in date
SELECT LAST_DAY(SYSDATE()) SELECT LAST_DAY(‘2015-02-02') SELECT LAST_DAY(‘2015-02-02 00:22:33')
-> 2016-01-31 -> 2015-02-28 -> 2015-02-28
12. MAKEDATE(year,dayofyear): Get the date based on the parameters (year, day)
SELECT MAKEDATE(2015 ,32)
-> 2015-02-01
13. MAKETIME(hour, minute, second): Get the time based on the parameters (hour, minute, second)
SELECT MAKETIME(12 ,23 ,34 )
-> 12:23:34
14. YEARWEEK(date): Get the year and week of the date
SELECT YEARWEEK(SYSDATE()) SELECT YEARWEEK(‘2015-01-10') SELECT YEARWEEK(‘2015-01-10',1)
-> 201602 -> 201501 -> 201502
15. WEEKOFYEAR(date): Get the week of the current year
SELECT WEEKOFYEAR(SYSDATE()) SELECT WEEKOFYEAR(‘2015-01-10')
-> 2 -> 2
mysql Several commonly used time format conversion functions are summarized as follows
1, from_unixtime(timestamp, format)
:
timestamp is int Type time, such as 14290450779; format is the converted format, including the following formats:
%M Month name (January...December)
%W Day of the week name (Sunday...Saturday)
%D Day of the month with English prefix (1st, 2nd, 3rd, etc. )
: The function is exactly the opposite of from_unixtime(). The former converts the unix timestamp into a readable time. And unix_timestamp() converts the readable time into a unix timestamp, which is used when sorting the time stored in datetime. For example, unix_timestamp('2009-08-06 10:10:40'), you get 1249524739.
3,
date_format(date, format):date_format()
If you want to know more about programming learning, please pay attention to the
column!
The above is the detailed content of What are the mysql date format conversion functions?. For more information, please follow other related articles on the PHP Chinese website!