Home >Database >Mysql Tutorial >How Do I Calculate the Time Difference Between Two Timestamps in MySQL?
Calculating the Time Difference Between Two Timestamps in MySQL
When working with timestamps, it's often useful to determine the difference between two time values. In MySQL, there are a couple of approaches you can take.
Using DATETIME Column
If you're using a DATETIME column to store timestamps, you can use simple subtraction to find the difference. However, the result will be in seconds. To convert it to days, you can divide by 86,400 (the number of seconds in a day).
<code class="sql">SELECT NOW(), last_confirmation_attempt, (NOW() - last_confirmation_attempt) / 86400 AS days_difference FROM DateClubs HAVING days_difference IS NOT NULL;</code>
Using DATEDIFF() Function
If you want to ignore the time portion of the timestamps and just get the difference in days, you can use the DATEDIFF() function.
<code class="sql">SELECT DATEDIFF('2010-10-08 18:23:13', '2010-09-21 21:40:36') AS days;</code>
The above is the detailed content of How Do I Calculate the Time Difference Between Two Timestamps in MySQL?. For more information, please follow other related articles on the PHP Chinese website!