Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the time difference function and date conversion calculation function in MySQL

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to use the time difference function and date conversion function in MySQL. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

1. Time difference function (TIMESTAMPDIFF, DATEDIFF)

Need to use MySQL to calculate the time difference, use TIMESTAMPDIFF, DATEDIFF, record the experimental results

-- 0select datediff (now (), now ());-- 2select datediff ('2015-04-22 23V 5900V,' 2015-04-2000RV 0000V);-- 2select datediff ('2015-04-2200VOV 00L,' 2015-04-2023VLV 5900');-- 1select TIMESTAMPDIFF (DAY, '2015-04-20 233RV 5900V,' 2015-04-220000Ru 0000') -- 2select TIMESTAMPDIFF (DAY, '2015-04-20 00 2select TIMESTAMPDIFF (DAY,' 2015-04-20 00);-- 2select TIMESTAMPDIFF (DAY, '2015-04-20 0000,' 2015-04-22 2323) 5900') -- 71select TIMESTAMPDIFF (HOUR, '2015-04-20 00,' 2015-04-22 23-00);-- 4260select TIMESTAMPDIFF (MINUTE, '2015-04-20 00, 2015-04-22 2300)

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

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

two。 Date conversion calculation functions (date_add, day, date_format, str_to_date)

-- convert date to string Calculate the first day of the current month and the first day of the next month select curdate () as' current date', DATE_FORMAT (curdate (),'% Ymuri% m') as' current month', str_to_date (concat (DATE_FORMAT (curdate (),'% YMY% m'),'- 01'),'% Ymuri% m% d') as' first day of the current month', date_add (concat (DATE_FORMAT (curdate () '% YMMI% m'),'-01'),'% YMMI% mMel% d'), interval 1 month) as' first day of next month' -- the last day of the current month select last_day (curdate ());-- the first day of next month select date_add (last_day (curdate ()), interval 1 day);-- what day is the day of the month select day (curdate ());-- the first day of the month select date_add (curdate (), interval 1-(day (curdate () day) On "MySQL time difference function, date conversion calculation function how to use" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report