MySQL time difference functions (TIMESTAMPDIFF, DATEDIFF), date conversion calculation functions (date_add, day, date_format, str_to_date)

MySQL time difference functions (TIMESTAMPDIFF, DATEDIFF), date conversion calculation functions (date_add, day, date_format, str_to_date)

1. Time difference functions (TIMESTAMPDIFF, DATEDIFF)

You need to use MySQL to calculate the time difference, use TIMESTAMPDIFF, DATEDIFF, and record the experimental results

--0
select datediff(now(), now());

--2
select datediff('2015-04-22 23:59:00', '2015-04-20 00:00:00');

--2
select datediff('2015-04-22 00:00:00', '2015-04-20 23:59:00');

--1
select TIMESTAMPDIFF(DAY, '2015-04-20 23:59:00', '2015-04-22 00:00:00');

--2
select TIMESTAMPDIFF(DAY, '2015-04-20 00:00:00', '2015-04-22 00:00:00');

--2
select TIMESTAMPDIFF(DAY, '2015-04-20 00:00:00', '2015-04-22 12:00:00');

--2
select TIMESTAMPDIFF(DAY, '2015-04-20 00:00:00', '2015-04-22 23:59:00');

--71
select TIMESTAMPDIFF(HOUR, '2015-04-20 00:00:00', '2015-04-22 23:00:00');

--4260
select TIMESTAMPDIFF(MINUTE, '2015-04-20 00:00:00', '2015-04-22 23:00:00');

For a description of TIMESTAMPDIFF, see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timestampdiff

If you want to know what other units TIMESTAMPDIFF can return data in, refer to the description of TIMESTAMPADD: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timestampadd

2. Date conversion calculation functions (date_add, day, date_format, str_to_date)

-- Use date and string conversion to calculate the first day of the current month and the first day of the next month select curdate() as 'current date', 
DATE_FORMAT(curdate(), '%Y-%m') as 'Current month', 
str_to_date(concat(DATE_FORMAT(curdate(), '%Y-%m'), '-01'), '%Y-%m-%d') as 'The first day of the current month', 
date_add(str_to_date(concat(DATE_FORMAT(curdate(), '%Y-%m'), '-01'), '%Y-%m-%d'), interval 1 month) as 'The first day of the next month';

-- The last day of the current month select last_day(curdate());

-- The first day of the next month select date_add(last_day(curdate()), interval 1 day);

-- What day of the month is the current day? select day(curdate());

-- First day of the month select date_add(curdate(), interval 1-(day(curdate())) day);

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Common date comparison and calculation functions in MySQL
  • mysql calculation function details

<<:  The best explanation of HTTPS

>>:  The use and methods of async and await in JavaScript

Recommend

How to remove the blue box that appears when the image is used as a hyperlink

I recently used Dreamweaver to make a product pres...

Docker installation and deployment of Net Core implementation process analysis

1. Docker installation and settings #Install Cent...

Vue3 gets the current routing address

Correct answer Using useRouter : // router path: ...

Commonly used English fonts for web page creation

Arial Arial is a sans-serif TrueType font distribu...

Docker+nacos+seata1.3.0 installation and usage configuration tutorial

I spent a day on it before this. Although Seata i...

favico.ico---Website ico icon setting steps

1. Download the successfully generated icon file, ...

How to install Maven automatically in Linux continuous integration

Unzip the Maven package tar xf apache-maven-3.5.4...

SQL implements LeetCode (180. Continuous numbers)

[LeetCode] 180. Consecutive Numbers Write a SQL q...

MySQL database basic syntax and operation

MySQL database basic syntax DDL Operations Create...

Vue implements infinite loading waterfall flow

This article example shares the specific code of ...

Summary of various methods of MySQL data recovery

Table of contents 1. Introduction 2. Direct recov...

Detailed explanation of the simple use of MySQL query cache

Table of contents 1. Implementation process of qu...

4 solutions to mysql import csv errors

This is to commemorate the 4 pitfalls I stepped o...

Encapsulate a simplest ErrorBoundary component to handle react exceptions

Preface Starting from React 16, the concept of Er...