The following article describes how to convert the date and time formats of Mysql, the date and specific time types of 7.3.6 can be seen in the description of the range of values related to different types and the valid format of the specified date and time values. Here is an example of using the mysql date function.
The following query Selects all records. The value of date_col is within the last 30 days:
- mysql> SELECT something FROM table
- WHERE TO_DAYS(NOW()) - TO_DAYS(date_col) <= 30;
- DAYOFWEEK(date)
Returns the index of the week of the date (1 = Sunday, 2 = Monday ,...... 7 = Saturday ). These index values correspond to the ODBC standard.
- mysql> select DAYOFWEEK('1998-02-03');
- -> 3
- WEEKDAY(date)
Returns the week index of date (0 = Monday, 1 = Tuesday ,...... 6 = Sunday ).
- mysql> select WEEKDAY('1997-10-04 22:23:00');
- -> 5
- mysql> select WEEKDAY('1997-11-05');
- -> 2
- DAYOFMONTH(date)
Returns the mysql date in the month of date in the range of 1 to 31.
- mysql> select DAYOFMONTH('1998-02-03');
- -> 3
- DAYOFYEAR(date)
Returns the number of days in a year from 1 to 366.
- mysql> select DAYOFYEAR('1998-02-03');
- -> 34
- MONTH(date)
Returns the month of date, ranging from 1 to 12.
- mysql> select MONTH('1998-02-03');
- -> 2
- DAYNAME(date)
Returns the week name of date.
- mysql> select DAYNAME("1998-02-05");
- -> 'Thursday'
- MONTHNAME(date)
Returns the month name of date.
- mysql> select MONTHNAME("1998-02-05");
- -> 'February'
- QUARTER(date)
Mysql returns the quarter of the year from date, ranging from 1 to 4.
- mysql> select QUARTER('98-04-01');
- -> 2
- WEEK(date)
- WEEK(date,first)
If Sunday is the first day of a week, there is a single parameter that returns the number of weeks of the date, ranging from 0 to 52. Two Parameter formats: WEEK () allows you to specify whether the WEEK starts on Sunday or Monday. If the second parameter is 0, the week starts from Sunday, and if the second parameter is 1, it starts from Monday.
- mysql> select WEEK('1998-02-20');
- -> 7
- mysql> select WEEK('1998-02-20',0);
- -> 7
- mysql> select WEEK('1998-02-20',1);
- -> 8
- YEAR(date)
Returns the year of date, ranging from 1000 to 9999.
- mysql> select YEAR('98-02-03');
- -> 1998
- HOUR(time)
The above content is an introduction to mysql date and time format conversion. I hope you will have some gains.