MOCKSTACKS
EN
Questions And Answers

More Tutorials









MYSQL Date and Time Operations

Date arithmetic


NOW() + INTERVAL 1 DAY -- This time tomorrow
CURDATE() - INTERVAL 4 DAY -- Midnight 4 mornings ago

Show the mysql questions stored that were asked 3 to 10 hours ago (180 to 600 minutes ago):

SELECT qId,askDate,minuteDiff
FROM
( SELECT qId,askDate,
 TIMESTAMPDIFF(MINUTE,askDate,now()) as minuteDiff
 FROM questions_mysql
) xDerived
WHERE minuteDiff BETWEEN 180 AND 600
ORDER BY qId DESC
LIMIT 50;
+----------+---------------------+------------+
| qId | askDate | minuteDiff |
+----------+---------------------+------------+
| 38546828 | 2016-07-23 22:06:50 | 182 |
| 38546733 | 2016-07-23 21:53:26 | 195 |
| 38546707 | 2016-07-23 21:48:46 | 200 |
| 38546687 | 2016-07-23 21:45:26 | 203 |
| ... | | |
+----------+---------------------+------------+

MySQL manual pages for TIMESTAMPDIFF().

Beware Do not try to use expressions like CURDATE() + 1 for date arithmetic in MySQL. They don't return what you expect, especially if you're accustomed to the Oracle database product. Use CURDATE() + INTERVAL 1 DAY instead.

Conclusion

In this page (written and validated by ) you learned about MYSQL Date and Time Operations . What's Next? If you are interested in completing MYSQL tutorial, your next topic will be learning about: MYSQL Handling Time Zones.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.