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

Specific use of MySQL operators (and, or, in, not)

Table of contents 1. Introduction 2. Main text 2....

JavaScript Basics Objects

Table of contents 1. Object 1.1 What is an object...

B2C website user experience detail design reference

Recently, when using Apple.com/Ebay.com/Amazon.co...

How to modify the default storage location of Docker images (solution)

Due to the initial partitioning of the system, th...

Linux centOS installation JDK and Tomcat tutorial

First download JDK. Here we use jdk-8u181-linux-x...

HTML validate HTML validation

HTML validate refers to HTML validation. It is the...

Docker Gitlab+Jenkins+Harbor builds a persistent platform operation

CI/CD Overview CI workflow design Git code versio...

Summary of Vue's common APIs and advanced APIs

Table of contents nextTick Mixins $forceUpdate se...

favico.ico---Website ico icon setting steps

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

Vue3 realizes the image magnifying glass effect

This article example shares the specific code of ...

VMware + Ubuntu18.04 Graphic Tutorial on Building Hadoop Cluster Environment

Table of contents Preface VMware clone virtual ma...

Search optimization knowledge to pay attention to in web design

1. Link layout of the new site homepage 1. The loc...

A brief discussion on how to use slots in Vue

How to define and use: Use the slot tag definitio...

How to run commands on a remote Linux system via SSH

Sometimes we may need to run some commands on a r...