MySQL的Data_ADD函数与日期格式化函数说明
今天看到了MySQL的日期函数,里面很多有用的,这里只把两个参数不太好记的粘下来了。
- DATE_ADD(date,INTERVAL expr type) DATE_SUB(date,INTERVAL expr type)
这些函数执行日期运算。 date 是一个 DATETIME 或DATE值,用来指定起始时间。 expr 是一个表达式,用来指定从起始日期添加或减去的时间间隔值。 Expr是一个字符串;对于负值的时间间隔,它可以以一个 ‘-'开头。 type 为关键词,它指示了表达式被解释的方式。
关键词INTERVA及 type 分类符均不区分大小写。
以下表显示了type 和expr 参数的关系:
MySQL 允许任何expr 格式中的标点分隔符。表中所显示的是建议的 分隔符。若 date 参数是一个 DATE 值,而你的计算只会包括 YEAR、MONTH和DAY部分(即, 没有时间部分), 其结果是一个DATE 值。否则,结果将是一个 DATETIME值。
若位于另一端的表达式是一个日期或日期时间值 , 则INTERVAL expr type只允许在 + 操作符的两端。对于 –操作符, INTERVAL expr type 只允许在其右端,原因是从一个时间间隔中提取一个日期或日期时间值是毫无意义的。 (见下面的例子)。
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 DATE_ADD('1992-12-31 23:59:59.000002',
-> INTERVAL '1.999999' SECOND_MICROSECOND);
-> '1993-01-01 00:00:01.000001'
若你指定了一个过于短的时间间隔值 (不包括type 关键词所预期的所有时间间隔部分), MySQL 假定你已经省去了时间间隔值的最左部分。 例如,你指定了一种类型的DAY_SECOND, expr 的值预期应当具有天、 小时、分钟和秒部分。若你指定了一个类似 '1:10'的值, MySQL 假定天和小时部分不存在,那么这个值代表分和秒。换言之, '1:10' DAY_SECOND 被解释为相当于 '1:10' MINUTE_SECOND。这相当于 MySQL将TIME 值解释为所耗费的时间而不是日时的解释方式。
假如你对一个日期值添加或减去一些含有时间部分的内容,则结果自动转化为一个日期时间值:
mysql> SELECT DATE_ADD('1999-01-01', INTERVAL 1 DAY);
-> '1999-01-02'
mysql> SELECT DATE_ADD('1999-01-01', INTERVAL 1 HOUR);
-> '1999-01-01 01:00:00'
假如你使用了格式严重错误的日期,则结果为 NULL。假如你添加了 MONTH、YEAR_MONTH或YEAR ,而结果日期中有一天的日期大于添加的月份的日期最大限度,则这个日期自动被调整为添加月份的最大日期:
mysql> SELECT DATE_ADD('1998-01-30', INTERVAL 1 MONTH);
-> '1998-02-28'
- DATE_FORMAT(date,format)
根据format 字符串安排date 值的格式。
以下说明符可用在 format 字符串中:
所有其它字符都被复制到结果中,无需作出解释。
注意, ‘%'字符要求在格式指定符之前。
月份和日期说明符的范围从零开始,原因是 MySQL允许存储诸如 '2004-00-00'的不完全日期.
mysql> <b>SELECT DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y');</b>
-> 'Saturday October 1997'
mysql> <b>SELECT DATE_FORMAT('1997-10-04 22:23:00', '%H:%i:%s');</b>
-> '22:23:00'
mysql> <b>SELECT DATE_FORMAT('1997-10-04 22:23:00',</b>
'%D %y %a %d %m %b %j');
-> '4th 97 Sat 04 10 Oct 277'
mysql> <b>SELECT DATE_FORMAT('1997-10-04 22:23:00',</b>
'%H %k %I %r %T %S %w');
-> '22 22 10 10:23:00 PM 22:23:00 00 6'
mysql> <b>SELECT DATE_FORMAT('1999-01-01', '%X %V');</b>
-> '1998 52'

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

In PHP language development, date formatting errors are a common problem. The correct date format is very important to programmers because it determines the readability, maintainability and correctness of the code. This article will share some tips for dealing with date formatting errors. Understanding date formats Before dealing with date formatting errors, we must first understand date formats. A date format is a string of various letters and symbols used to represent a specific date and time format. In PHP, common date formats include: Y: four-digit year (such as 20

Python regular expression is a very powerful text processing tool that can perform operations such as matching, replacing, and extracting strings. In actual development, we often need to format dates, such as converting "2022/10/01" into the format of "October 01, 2022". This article will introduce how to use Python regular expressions for date formatting. 1. Overview of Python regular expressions Python regular expression is a special string pattern, which describes a series of characters that match a certain

Java is a popular programming language that includes many powerful tools for date and time manipulation. One of the most commonly used tools is the DateFormat function, which can format date data into a specific string format. This article will introduce how to use the DateFormat function in Java for date formatting. Import the Date and DateFormat classes. Before starting to use the DateFormat function, we need to import the Date and DateFormat classes in Java.

PHP Time Processing Tips: Quickly Calculate Time Difference and Date Formatting With the rapid development of the Internet, time processing has become one of the common tasks in web development. In PHP, time processing is a relatively common requirement, such as calculating time differences, formatting dates, and other operations. This article will introduce some PHP time processing techniques, including quickly calculating time differences and date formatting, and come with some specific code examples. Calculating the time difference In many application scenarios, we need to calculate the time difference between two time points, such as calculating the time difference between two time points.

How to use date and time functions in Java for date calculation and formatting In Java, date and time are very common and important data types. In order to facilitate the processing of dates and times, Java provides a wealth of date and time functions that can perform date calculation, formatting and other operations. Below is a detailed introduction to how to use date and time functions in Java, along with code examples. 1. Date calculation to obtain the current date. Use the java.time.LocalDate class to obtain the current date. Example code

Solution Overview to Solve Java Date Formatting Exception (DateTimeParseException): In Java, date formatting is a common task. We can convert date and time to a specified format by using the SimpleDateFormat class or the new date time APIs available in Java 8 (such as DateTimeFormatter). However, sometimes when formatting dates, you may encounter DateTimePars

When developing a website, you often need to work with dates and times. For example, you might want to display the date a post was last modified or mention when a reader left a comment. You may also want to display a countdown until a special event occurs. Luckily, PHP comes with some built-in date and time functions that will help us do all this easily. This tutorial will teach you how to format the current date and time in PHP. You will also learn how to get a timestamp from a date string and how to add and subtract different dates. Get date and time in string format date($format,$timestamp) is one of the most commonly used date and time functions in PHP. It takes the desired date output format as the first parameter and will

How to solve date processing problems in Java development Date processing is an important problem often encountered in Java development. In the actual development process, we often need to perform date comparison, format conversion, calculation and other operations. There are also quite a lot of details and pitfalls involved in date processing. Here are some common date processing problems and their solutions. 1. Date formatting In Java, date formatting mainly relies on the SimpleDateFormat class. However, when formatting dates, a problem often occurs
