Home >Database >Mysql Tutorial >What are the mysql date conversion functions?

What are the mysql date conversion functions?

青灯夜游
青灯夜游Original
2021-12-02 15:20:4622055browse

Mysql date conversion functions are: 1. date_format(); 2. time_format(); 3. str_to_date(); 4. to_days(); 5. from_days(); 6. time_to_sec(); 7. sec_to_time() etc.

What are the mysql date conversion functions?

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

MySQL date conversion function, time conversion function

1, MySQL Date/Time to Str (date/time converted to string) function: date_format( date,format), time_format(time,format)

Function: date_format('2008-08-08 22:23:01', '%Y %m%d%H%i%s')

Result: 20080808222301

MySQL date and time conversion function: date_format (date,format), time_format(time,format) can convert a date/time into various string formats. It is an inverse conversion of the str_to_date(str,format) function.

2, MySQL Str to Date (convert string to date) function: str_to_date(str, format)

select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09
select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09
select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09
select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30
select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30

str_to_date(str,format) Conversion function can convert some messy strings into date format.

3, MySQL (date, number of days) conversion function: to_days(date), from_days(days):

select to_days('0000-00-00'); -- 0
select to_days('2008-08-08'); -- 733627

4, MySQL ( time, seconds) conversion function: time_to_sec(time), sec_to_time(seconds):

select time_to_sec('01:00:05'); -- 3605
select sec_to_time(3605); -- '01:00:05'

5, MySQL piece together date and time function: makdedate( year,dayofyear), maketime(hour,minute,second)

select makedate(2001,31); -- '2001-01-31'
select makedate(2001,32); -- '2001-02-01'
select maketime(12,15,30); -- '12:15:30'

6, MySQL (Unix timestamp, date) conversion function:

unix_timestamp(),
unix_timestamp(date),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)

[Related recommendations: mysql video tutorial]

The above is the detailed content of What are the mysql date conversion functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn