Let's talk about commonly used date-related functions in MySQL
Free learning recommendation: mysql video tutorial
##Table of contents
- Part 1: Time difference functions timestampdiff, datediff, timediff
- ##1. Time difference function: timestampdiff
- 2 , Time difference function: datediff
- 3. Time difference function: timediff
- 4. Other date functions
- Other date functions
- Others
1. Time difference function: timestampdiff
Syntax: timestampdiff(interval, datetime1,datetime2)
Result: Returns the time difference of (time 2 - time 1), the result unit is given by the interval parameter.
frac_second milliseconds (lower versions do not support it, use second, and then divide by 1000) second second minute minute
hour hour
day day
week week
month month
quarter quarter
year year
Note: Millisecond recording and calculation are only supported after MySQL 5.6. If it is before For the version, it is best to create an int field for storing milliseconds in the fields other than the datetime type in the database, and then perform the conversion calculation yourself. # All formatsSELECT TIMESTAMPDIFF(FRAC_SECOND,'2012-10-01','2013-01-13'); # 暂不支持SELECT TIMESTAMPDIFF(SECOND,'2012-10-01','2013-01-13'); # 8985600SELECT TIMESTAMPDIFF(MINUTE,'2012-10-01','2013-01-13'); # 149760SELECT TIMESTAMPDIFF(HOUR,'2012-10-01','2013-01-13'); # 2496SELECT TIMESTAMPDIFF(DAY,'2012-10-01','2013-01-13'); # 104SELECT TIMESTAMPDIFF(WEEK,'2012-10-01','2013-01-13'); # 14SELECT TIMESTAMPDIFF(MONTH,'2012-10-01','2013-01-13'); # 3SELECT TIMESTAMPDIFF(QUARTER,'2012-10-01','2013-01-13'); # 1SELECT TIMESTAMPDIFF(YEAR,'2012-10-01','2013-01-13'); # 0Copy after login
2. Time difference function: datediff
Syntax: SELECT DATEDIFF(date 1, date 2)
Result: The difference in days between date 1 - date 2SLECT DATEDIFF('2013-01-13','2012-10-01'); # 104
3. Time difference function: timediff
Syntax: timediff(time1,time2)
Result: Return the difference between time1-time2 SELECT TIMEDIFF('2018-05-21 14:51:43','2018-05-19 12:54:43');#49:57:00
Note: The two parameters of this method must be in date format
4. Other date functions
The now() function returns the year, month, day, hour, minute and second of the current time.
- The curdate() function returns the year, month, day information.
- curtime( ) function returns the hour, minute, and second information of the current time.
- To format a date containing the year, month, day, hour, minute, and second into a month, day, and date, you can use the DATE(time) function.
- Other date functions
SELECT NOW(); # 2018-05-21 14:41:00SELECT CURDATE(); # 2018-05-21SELECT CURTIME(); # 14:41:38SELECT DATE(NOW()); # 2018-05-21SELECT SYSDATE(); # 2018-05-21 14:47:11SELECT CURRENT_TIME(); # 14:51:30SELECT CURRENT_TIMESTAMP; # 2018-05-21 14:51:37SELECT CURRENT_TIMESTAMP(); # 2018-05-21 14:51:43
Part 2 Date, timestamp, string conversion
#时间日期转字符串 相当与Oracle中的to_char函数select date_format(now(), '%Y-%m-%d'); #结果:2017-02-27#时间转时间戳select unix_timestamp(now()); #结果:1488160428#字符串转时间select str_to_date('2017-02-27', '%Y-%m-%d %H'); #结果:2017-02-27 00:00:00select str_to_date('2017-10-16 15:30:28','%Y-%m-%d %H:%i:%s'); #结果 2017-10-16 15:30:28#注意 年是大写‘Y’,小时也必须是大写‘H’ (如果其他为大写,则得到结果为null)#字符串转时间戳select unix_timestamp('2017-02-27'); #结果:1488124800#时间戳转时间select from_unixtime(1488160428); #结果:2017-02-27 09:53:48#时间戳转字符串select from_unixtime(1488160428,'%Y-%m-%d %T'); #结果:2017-02-27 09:53:48 SELECT DATE_FORMAT(CURDATE(),'%Y-%m-%d %H:%i:%s'); -- 获取当天零点select curdate(); --获取当前日期select last_day(curdate()); --获取当月最后一天。select DATE_ADD(curdate(),interval -day(curdate())+1 day); --获取本月第一天select date_add(curdate()-day(curdate())+1,interval 1 month); -- 获取下个月的第一天select DATEDIFF(date_add(curdate()-day(curdate())+1,interval 1 month ),DATE_ADD(curdate(),interval -day(curdate())+1 day)) from dual; --获取当前月的天数
SELECT DAY('2021-2-17') -- 当前月的第几天(17)SELECT MONTH('2021-2-17') -- 当前为几月(2)
mysql tutorial(Video)
The above is the detailed content of Let's talk about commonly used date-related functions in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.
