Home Database Mysql Tutorial MySQL(基础篇)之MySQL(时间和日期函数)

MySQL(基础篇)之MySQL(时间和日期函数)

Jun 07, 2016 pm 03:03 PM
mysql function Base date time

一、日期和时间函数 DAYOFWEEK(date) 返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准) WEEKDAY(date) 返回日期date是星期几(0=星期一,1=星期二,……6= 星期天)。 MONTH(date) 返回date中的月份数 DAYNAME(date) 返回date是星期几(按英文名返



一、日期和时间函数

DAYOFWEEK(date)
 返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)

MySQL(基础篇)之MySQL(时间和日期函数)

WEEKDAY(date)
 返回日期date是星期几(0=星期一,1=星期二,……6= 星期天)。

MySQL(基础篇)之MySQL(时间和日期函数)

MONTH(date)
 返回date中的月份数值

MySQL(基础篇)之MySQL(时间和日期函数)

DAYNAME(date)
 返回date是星期几(按英文名返回)

MySQL(基础篇)之MySQL(时间和日期函数)

MONTHNAME(date)
 返回date是几月(按英文名返回)

MySQL(基础篇)之MySQL(时间和日期函数)

QUARTER(date)
 返回date是一年的第几个季度

MySQL(基础篇)之MySQL(时间和日期函数)

WEEK(date,first)
 返回date是一年的第几周(first默认值0,first取值1表示周一是周的开始,0从周日开始)

MySQL(基础篇)之MySQL(时间和日期函数)

YEAR(date)
 返回date的年份(范围在1000到9999)

MySQL(基础篇)之MySQL(时间和日期函数)

HOUR(time)
 返回time的小时数(范围是0到23)

MySQL(基础篇)之MySQL(时间和日期函数)

MINUTE(time)
 返回time的分钟数(范围是0到59)

MySQL(基础篇)之MySQL(时间和日期函数)

SECOND(time)
 返回time的秒数(范围是0到59)

MySQL(基础篇)之MySQL(时间和日期函数)

PERIOD_ADD(P,N)
 增加N个月到时期P并返回(P的格式YYMM或YYYYMM

MySQL(基础篇)之MySQL(时间和日期函数)

PERIOD_DIFF(P1,P2)
 返回在时期P1和P2之间月数(P1和P2的格式YYMM或YYYYMM

PERIOD_DIFF(P1,P2)
 返回在时期P1和P2之间月数(P1和P2的格式YYMM或YYYYMM)
mysql> select PERIOD_DIFF(9802,199703);
  -> 11
DATE_ADD(date,INTERVAL expr type)
DATE_SUB(date,INTERVAL expr type)
ADDDATE(date,INTERVAL expr type)
SUBDATE(date,INTERVAL expr type)
 对日期时间进行加减法运算
 (ADDDATE()和SUBDATE()是DATE_ADD()和DATE_SUB()的同义词,也可以用运算符 和-而不是函数
 date是一个DATETIME或DATE值,expr对date进行加减法的一个表达式字符串type指明表达式expr应该如何被解释
 [type值 含义 期望的expr格式]:
 SECOND 秒 SECONDS
 MINUTE 分钟 MINUTES
 HOUR 时间 HOURS
 DAY 天 DAYS
 MONTH 月 MONTHS
 YEAR 年 YEARS
 MINUTE_SECOND 分钟和秒 "MINUTES:SECONDS"
 HOUR_MINUTE 小时和分钟 "HOURS:MINUTES"
 DAY_HOUR 天和小时 "DAYS HOURS"
 YEAR_MONTH 年和月 "YEARS-MONTHS"
 HOUR_SECOND 小时, 分钟, "HOURS:MINUTES:SECONDS"
 DAY_MINUTE 天, 小时, 分钟 "DAYS HOURS:MINUTES"
 DAY_SECOND 天, 小时, 分钟, 秒 "DAYS HOURS:MINUTES:SECONDS"
 expr中允许任何标点做分隔符,如果所有是DATE值时结果是一个DATE值,否则结果是一个DATETIME值)
 如果type关键词不完整,则MySQL从右端取值,DAY_SECOND因为缺少小时分钟等于MINUTE_SECOND)
 如果增加MONTH、YEAR_MONTH或YEAR,天数大于结果月份的最大天数则使用最大天数)
mysql> SELECT "1997-12-31 23:59:59" INTERVAL 1 SECOND;
  -> 1998-01-01 00:00:00
mysql> SELECT INTERVAL 1 DAY "1997-12-31";
  -> 1998-01-01
mysql> SELECT "1998-01-01" - INTERVAL 1 SECOND;
  -> 1997-12-31 23:59:59
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",INTERVAL 1 SECOND);
  -> 1998-01-01 00:00:00
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",INTERVAL 1 DAY);
  -> 1998-01-01 23:59:59
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",INTERVAL "1:1" MINUTE_SECOND);
  -> 1998-01-01 00:01:00
mysql> SELECT DATE_SUB("1998-01-01 00:00:00",INTERVAL "1 1:1:1" DAY_SECOND);
  -> 1997-12-30 22:58:59
mysql> SELECT DATE_ADD("1998-01-01 00:00:00", INTERVAL "-1 10" DAY_HOUR);
  -> 1997-12-30 14:00:00
mysql> SELECT DATE_SUB("1998-01-02", INTERVAL 31 DAY);
  -> 1997-12-02
mysql> SELECT EXTRACT(YEAR FROM "1999-07-02");
  -> 1999
mysql> SELECT EXTRACT(YEAR_MONTH FROM "1999-07-02 01:02:03");
  -> 199907
mysql> SELECT EXTRACT(DAY_MINUTE FROM "1999-07-02 01:02:03");
  -> 20102

TO_DAYS(date)
 返回日期date是西元0年至今多少天(不计算1582年以前)

MySQL(基础篇)之MySQL(时间和日期函数)

FROM_DAYS(N)
 给出西元0年至今多少天返回DATE值(不计算1582年以前)

MySQL(基础篇)之MySQL(时间和日期函数)

DATE_FORMAT(date,format)
 根据format字符串格式化date值
 (在format字符串中可用标志符:
 %M 月名字(January……December)
 %W 星期名字(Sunday……Saturday)
 %D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
 %Y 年, 数字, 4 位
 %y 年, 数字, 2 位
 %a 缩写的星期名字(Sun……Sat)
 %d 月份中的天数, 数字(00……31)
 %e 月份中的天数, 数字(0……31)
 %m 月, 数字(01……12)
 %c 月, 数字(1……12)
 %b 缩写的月份名字(Jan……Dec)
 %j 一年中的天数(001……366)
 %H 小时(00……23)
 %k 小时(0……23)
 %h 小时(01……12)
 %I 小时(01……12)
 %l 小时(1……12)
 %i 分钟, 数字(00……59)
 %r 时间,12 小时(hh:mm:ss [AP]M)
 %T 时间,24 小时(hh:mm:ss)
 %S 秒(00……59)
 %s 秒(00……59)
 %p AM或PM
 %w 一个星期中的天数(0=Sunday ……6=Saturday )
 %U 星期(0……52), 这里星期天是星期的第一天
 %u 星期(0……52), 这里星期一是星期的第一天
 %% 字符% )
mysql> select DATE_FORMAT('1997-10-04 22:23:00','%W %M %Y');
  -> 'Saturday October 1997'
mysql> select DATE_FORMAT('1997-10-04 22:23:00','%H:%i:%s');
  -> '22:23:00'
mysql> select DATE_FORMAT('1997-10-04 22:23:00','%D %y %a %d %m %b %j');
  -> '4th 97 Sat 04 10 Oct 277'
mysql> select DATE_FORMAT('1997-10-04 22:23:00','%H %k %I %r %T %S %w');
  -> '22 22 10 10:23:00 PM 22:23:00 00 6'
TIME_FORMAT(time,format)
 和DATE_FORMAT()类似,但TIME_FORMAT只处理小时、分钟和秒(其余符号产生一个NULL值或0)


CURTIME()
CURRENT_TIME()
 以'HH:MM:SS'或HHMMSS格式返回当前时间值(根据返回值所处上下文是字符串或数字)

MySQL(基础篇)之MySQL(时间和日期函数)

FROM_UNIXTIME(unix_timestamp)
 以'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式返回时间戳的值(根据返回值所处上下文是字符串或数字)

MySQL(基础篇)之MySQL(时间和日期函数)

FROM_UNIXTIME(unix_timestamp,format)
 以format字符串格式返回时间戳的值
mysql> select FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x');
  -> '1997 23rd December 03:43:30 x'
SEC_TO_TIME(seconds)
 以'HH:MM:SS'或HHMMSS格式返回秒数转成的TIME值(根据返回值所处上下文是字符串或数字)
mysql> select SEC_TO_TIME(2378);
  -> '00:39:38'
mysql> select SEC_TO_TIME(2378) 0;
  -> 3938
TIME_TO_SEC(time)
 返回time值有多少秒
mysql> select TIME_TO_SEC('22:23:00');
  -> 80580
mysql> select TIME_TO_SEC('00:39:38');
  -> 2378


Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

See all articles