Home Database Mysql Tutorial mysql时间与日期函数_MySQL

mysql时间与日期函数_MySQL

May 27, 2016 pm 01:46 PM
function date time

返回日期相关的

 

 

 

Now() || CURRENT_TIMESTAMP();返回当前时间

 

 

 

to_days(date) 返回日期date是西元0年至今多少天(不计算1582年以前) 转换为天数 date是日期类型

 

 

 

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

 

 

 

CURDATE()||CURRENT_DATE()||SYSDATE()  当前日期 //注意大写

 

 

 

weekDay(date) 返回指定日期的星期几  //参数是日期型,不能是时间戳格式

 

 

 

DAYOFWEEK(date) 返回指定日期的星期几 //参数是日期型,不能是时间戳格式

 

 

 

DAYNAME(date) 返回指定日期的星期几,具体到英文单词  //参数是日期型,不能是时间戳格式

 

 

 

WEEK(date,first)//参数是日期型,不能是时间戳格式 返回date是一年的第几周(first默认值0,first取值1表示周一是周的开始,0从周日开始)

 

 

 

DAYOFMONTH(date) 返回指定日期是当月的几号 //参数是日期型,不能是时间戳格式

 

 

 

MONTH(date)  返回指定日期的月份 //参数是日期型,不能是时间戳格式

 

 

 

MONTHNAME  返回date是几月(按英文名返回)//参数是日期型,不能是时间戳格式

 

 

 

QUARTER(date) 返回date是一年的第几个季度  //参数是日期型,不能是时间戳格式

 

 

 

YEAR(date)  返回date年份  //参数是日期型,不能是时间戳格式

 

 

 

 

 

DATE_FORMAT(Date,format);根据format字符串格式化date值

 

 

 

select DATE_FORMAT(CURDATE(),'%W %M %Y');

 

%W 代表英文归属星期几

 

%M 代表英文归属月份

 

%Y 代表英文归属年份

 

 

 

select DATE_FORMAT(Now(),'%H:%i:%s');

 

%H 返回小时数

 

%i 返回分钟数

 

%s 返回秒数

 

 

 

select DATE_FORMAT(Now(),'%D %y %a %d %m %b %j')

 

%j 一年中的天数(001……366)

 

%b 缩写的月份名字(Jan……Dec)

 

%m 月, 数字(01……12)

 

%d 月份中的天数, 数字(00……31)

 

%a 缩写的星期名字(Sun……Sat)

 

%y 年, 数字, 2 位

 

%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)

 

 

 

 

 

select DATE_FORMAT(Now(),'%H %k %I %r %T %S %w');   

 

%k 小时(0……23)

 

%l 小时(1……12)

 

%r 时间,12 小时(hh:mm:ss [AP]M)

 

%T 时间,24 小时(hh:mm:ss)

 

%S 秒(00……59)

 

%w 一个星期中的天数(0=Sunday ……6=Saturday )

 

 

 

%e 月份中的天数, 数字(0……31)

 

%b 缩写的月份名字(Jan……Dec)

 

%I 小时(01……12)

 

%s 秒(00……59)

 

%p AM或PM

 

%U 星期(0……52), 这里星期天是星期的第一天

 

%u 星期(0……52), 这里星期一是星期的第一天

 

%% 字符%

 

 

 

 

 

返回时间相关的

 

CURTIME()  返回当前时间

 

 

 

HOUR(); 返回小时数                //参数是日期型,不能是时间戳格式

 

 

 

MINUTE(time)  返回分钟数        //参数是日期型,不能是时间戳格式

 

 

 

SECOND(time) 返回秒数                //参数是日期型,不能是时间戳格式

 

 

 

日期的加减运算

 

 

 

DATE_ADD(date,INTERVAL expr type)  加法运算

 

DATE_SUB(date,INTERVAL expr type)  减法运算

 

ADDDATE(date,INTERVAL expr type)   加法

 

SUBDATE(date,INTERVAL expr type)   减法

 

对日期时间进行加减法运算

 

expr type 类型

 

 

 

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"

 

 

 

 

 

 

 

SELECT ADDDATE(NOW(),INTERVAL 5 DAY);

 

SELECT DATE_ADD(NOW(),INTERVAL 5 DAY);

 

 

 

SELECT DATE_SUB(NOW(),INTERVAL 5 DAY);

 

SELECT SUBDATE(NOW(),INTERVAL 5 DAY);

 

 

 

UNIX_TIMESTAMP() UNIX_TIMESTAMP(Date );  返回一个Unix时间戳(从'1970-01-01 00:00:00'GMT开始的秒数,就是秒数

 

 

 

FROM_UNIXTIME(UNIX_TIMESTAMP()) ;  根据这个秒数返回制定日期的字符串

 

 

 

FROM_UNIXTIME(unix_timestamp,format)    自己制定格式化,返回制定日期的字符串

 

 

 

TIME_TO_SEC(time)  返回time值有多少秒

 

 

 

SEC_TO_TIME(seconds) 以'HH:MM:SS'或HHMMSS格式返回秒数转成的TIME值(根据返回值所处上下文是字符串或数字)

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Apr 21, 2024 am 10:21 AM

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

What is the difference between custom PHP functions and predefined functions? What is the difference between custom PHP functions and predefined functions? Apr 22, 2024 pm 02:21 PM

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.

C++ Function Exception Advanced: Customized Error Handling C++ Function Exception Advanced: Customized Error Handling May 01, 2024 pm 06:39 PM

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

See all articles