Home  >  Article  >  Database  >  How to Calculate the Difference Between Two Timestamps in Days in MySQL?

How to Calculate the Difference Between Two Timestamps in Days in MySQL?

Linda Hamilton
Linda HamiltonOriginal
2024-11-02 03:30:30782browse

How to Calculate the Difference Between Two Timestamps in Days in MySQL?

Getting the Difference Between Two Timestamps in Days in MySQL

When working with timestamps in MySQL, it can be useful to calculate the difference between two timestamps. By default, subtracting two timestamps will result in the difference in seconds, which may not be the desired result.

To calculate the difference in days, ignoring the time portion of the timestamps, you can use the DATEDIFF() function. This function takes two timestamps as its arguments and returns the number of days between them.

Here's an example:

SELECT DATEDIFF('2023-04-10 12:30:00', '2023-03-25 18:25:54') AS days;

This query will return 15, as there are 15 days between the two timestamps.

To demonstrate the usage, let's use your provided MySQL statement:

SELECT NOW(), last_confirmation_attempt, DATEDIFF(NOW(), last_confirmation_attempt) AS days FROM DateClubs WHERE DATEDIFF(NOW(), last_confirmation_attempt) IS NOT NULL;

This modified query will provide the difference in days between NOW() and last_confirmation_attempt for each row in the DateClubs table where the difference is not NULL.

The above is the detailed content of How to Calculate the Difference Between Two Timestamps in Days in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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