Home Database Mysql Tutorial mysql日期时间常用函数总结

mysql日期时间常用函数总结

Jun 07, 2016 pm 05:53 PM
date time

在mysql中我们经常会用到一些常用的时间与日期一些运算,下面我来总结一下关于mysql日期时间常用函数介绍,有需要的朋友参考。

一、时间差

datediff:说白了就是用第一个时间去减第二个时间,顺序不能忘记

 代码如下 复制代码

select datediff('2012-08-08', '2012-08-13'); -- -5

二、获取当前时间

 代码如下 复制代码

now
current_timestamp()
,current_timestamp
,localtime()
,localtime
,localtimestamp    -- (v4.0.6)
,localtimestamp()  -- (v4.0.6)

sysdate(); //动态获取系统时间。这个要和now函数区分来,now就好比已经定义了一个变量,sysdate等于变量是在运行到后确定,所以他们有一个时差


三、获得当前日期(date)函数

curdate()

以'YYYY-MM-DD'或YYYYMMDD格式返回今天日期值,取决于函数是在一个字符串还是数字上下文被使用。

 代码如下 复制代码

mysql> select CURDATE();

+------------+

| CURDATE()  |

+------------+

| 2001-02-20 |

+------------+


current_date()


四、获得当前时间

curtime()

CURTIME() 返回当前时间

以'HH:MM:SS'或HHMMSS格式返回当前时间值,取决于函数是在一个字符串还是在数字的上下文被使用。

 代码如下 复制代码

mysql> select CURTIME();

+-----------+

| CURTIME() |

+-----------+

| 10:42:38  |

+-----------+


current_time()
current_time

五、获得当前 UTC 日期时间函数

 代码如下 复制代码

select utc_timestamp(), utc_date(), utc_time(), now()

六、Mysql日期时间函数

 代码如下 复制代码

set @dt = '2008-09-10 07:15:30.123456';
 
select date(@dt);        -- 2008-09-10
select time(@dt);        -- 07:15:30.123456
select year(@dt);        -- 2008
select quarter(@dt);     -- 3
select month(@dt);       -- 9
select week(@dt);        -- 36
select day(@dt);         -- 10
select hour(@dt);        -- 7
select minute(@dt);      -- 15
select second(@dt);      -- 30
select microsecond(@dt); -- 123456


下面分离一些年月日函数

下面的查询选择了所有记录,其date_col的值是在最后30天以内:

 代码如下 复制代码

mysql> SELECT something FROM table
WHERE TO_DAYS(NOW()) - TO_DAYS(date_col)

DAYOFWEEK(date)

返回日期date的星期索引(1=星期天,2=星期一, ……7=星期六)。这些索引值对应于ODBC标准。

 代码如下 复制代码

mysql> select DAYOFWEEK('1998-02-03'/res/images/smilies/wink.gif;
-> 3

WEEKDAY(date)

返回date的星期索引(0=星期一,1=星期二, ……6= 星期天)。

 代码如下 复制代码

mysql> select WEEKDAY('1997-10-04 22:23:00'/res/images/smilies/wink.gif;

-> 5

mysql> select WEEKDAY('1997-11-05'/res/images/smilies/wink.gif;

-> 2

 

DAYOFMONTH(date)

返回date的月份中日期,在1到31范围内。

 代码如下 复制代码

mysql> select DAYOFMONTH('1998-02-03'/res/images/smilies/wink.gif;

-> 3

DAYOFYEAR(date)

返回date在一年中的日数, 在1到366范围内。

 代码如下 复制代码

mysql> select DAYOFYEAR('1998-02-03'/res/images/smilies/wink.gif;

-> 34

MONTH(date)

返回date的月份,范围1到12。

 代码如下 复制代码

mysql> select MONTH('1998-02-03'/res/images/smilies/wink.gif;

-> 2

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 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)

How to extract a specified part of a datetime using MySQL's EXTRACT function How to extract a specified part of a datetime using MySQL's EXTRACT function Jul 26, 2023 am 09:11 AM

How to use MySQL's EXTRACT function to extract a specified part of a date and time. MySQL database is one of the most commonly used relational databases and is widely used in various types of applications. Date and time are common data types in databases, and it is often necessary to extract specific parts from date and time fields to meet business needs. In MySQL, you can use the EXTRACT function to extract a specified part from a datetime. This article will introduce how to use MySQL's EXTRACT function to extract date time

How to handle date and time using PHP built-in functions? How to handle date and time using PHP built-in functions? Apr 22, 2024 pm 05:03 PM

PHP built-in functions provide a convenient way to handle dates and times. You can use these functions to create, format, and manipulate datetime values, for example: To create a datetime value: use the newDateTime() function. Format datetime values: use the format() method. Manipulate date and time values: use add(), sub(), setTimestamp() and other methods. Compare datetime values: Use comparison operators. Calculate the difference between dates: use the diff() method.

Golang time format conversion: tips for converting date and time Golang time format conversion: tips for converting date and time Feb 24, 2024 pm 06:21 PM

Golang time conversion: Tips for converting dates and times to each other. As the Go language continues to develop in various fields, the need to process dates and times is becoming more and more common. In the Go language, date and time conversion is a basic operation and a common requirement in development. This article will introduce how to convert date and time in Golang, and provide specific code examples to help developers better master this technique. Representation of date and time In Go language, date and time are represented by time.Time respectively.

PHP 5.5 date time processing: How to use DateTime class for date time operations PHP 5.5 date time processing: How to use DateTime class for date time operations Jul 31, 2023 pm 05:41 PM

PHP5.5 date and time processing: How to use the DateTime class for date and time operations In PHP development, processing date and time is a very common requirement. PHP5.5 introduces a very powerful date and time processing class-DateTime, which provides some convenient methods to manipulate dates and times. In this article, we will introduce how to use DateTime class for date time operations. Creating a DateTime object Instantiation of the DateTime class is very simple, just

Format date and time using java's String.format() function Format date and time using java's String.format() function Jul 25, 2023 pm 08:49 PM

Introduction to formatting date and time using Java's String.format() function: In Java, we often need to format date and time to meet specific style requirements. Java provides multiple ways to format date and time, one of the commonly used ways is to use the String.format() function. This article will introduce how to use the String.format() function to format date and time and provide code examples. Code Example: Below is a simple example that demonstrates

How to use the datetime module to get the current date and time in Python 3.x How to use the datetime module to get the current date and time in Python 3.x Jul 31, 2023 pm 03:33 PM

How to use the datetime module to obtain the current date and time in Python3.x In Python programming, the datetime module can be used to easily obtain the current date and time. The datetime module provides various functions and classes to handle dates, times, and intervals. In order to use the datetime module, you first need to import the module: importdatetime The way to get the current date and time is to use datetim in the datetime module

Timestamp handling in PHP: How to convert timestamp to datetime using date function Timestamp handling in PHP: How to convert timestamp to datetime using date function Jul 29, 2023 pm 06:01 PM

Timestamp handling in PHP: How to convert timestamp to datetime using date function Timestamp is a common way used to represent dates and times. In PHP, we can use the built-in date function to convert the timestamp into an easy-to-read date and time format. This article will introduce how to use the date function to process timestamps and provide some sample code for reference. First, let's understand what a timestamp is. The timestamp refers to the time since January 1, 1970 00:00:00

Java Error: Java8 Date Time Processing Error, How to Handle and Avoid Java Error: Java8 Date Time Processing Error, How to Handle and Avoid Jun 24, 2023 pm 12:44 PM

Java8 is a very popular programming language and is widely used in many companies and projects. One of its most interesting new features is the DateTime API, which provides a completely new way to work with dates and times. However, there are also some problems during use, and Java8 date and time processing errors are one of them. This article will explore some common Java8 date and time processing errors and provide corresponding solutions and avoidance methods. Date formatting error Date formatting is a problem in Java8

See all articles