How to subtract 30 days from the current datetime in mysql?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Subtracting 30 days from the current date and time in MySQL is a standard date-arithmetic task. The usual answer is DATE_SUB(NOW(), INTERVAL 30 DAY), but it is also useful to understand how the choice between NOW(), CURDATE(), and predicate shape affects the result.
Use DATE_SUB with NOW()
If you want the current timestamp minus 30 days, use NOW() together with DATE_SUB.
NOW() returns the current date and time, so the result keeps both parts.
You can write the same calculation with interval syntax as well:
Both are common in MySQL. The DATE_SUB form is often easier to read when the query contains several date manipulations.
Know Whether You Want a Datetime or a Date
If the question is really about the last 30 calendar days rather than the last 30 times 24 hours, CURDATE() may be a better starting point.
CURDATE() returns only the date, with no time component. That difference matters in reporting queries. For example, filtering with a date-only cutoff can include or exclude records differently from filtering with a full timestamp cutoff.
Use It in WHERE Clauses Carefully
A very common use case is retrieving recent rows.
This means "rows from the last 30 times 24 hours." That is different from "all rows from the current day and the previous 29 days," which is a calendar-based interpretation.
For old-row cleanup, flip the comparison:
The calculation itself is simple. The real engineering question is which cutoff semantics your application needs.
Indexing and Predicate Shape
Avoid wrapping the column in a function when you want index-friendly filtering. This is usually better:
than this:
The second form can make indexing less useful because the function is applied to every row value before comparison. When performance matters, compute the cutoff once and compare the column directly.
DATE_ADD with a Negative Interval Also Works
MySQL also allows subtraction through DATE_ADD and a negative interval.
This is valid, but most readers find DATE_SUB(..., INTERVAL 30 DAY) clearer because it says exactly what the query is doing.
Time zone policy also matters. If the application server, MySQL session, and business reporting rules disagree about the active time zone, a perfectly valid date-arithmetic expression can still produce the wrong business result.
Testing with representative sample timestamps around midnight, month boundaries, and daylight-saving changes can catch these semantic mistakes early, especially in reporting or retention code where date rules often carry business meaning beyond simple arithmetic.
Common Pitfalls
Confusing NOW() with CURDATE() leads to subtle bugs because one includes time and the other does not.
Using the wrong comparison direction in retention queries can reverse the meaning of the filter.
Wrapping the datetime column in a function during filtering can make the query less index-friendly.
Summary
- Use
DATE_SUB(NOW(), INTERVAL 30 DAY)for the current timestamp minus 30 days. - Use
CURDATE()instead ofNOW()when you want date-only semantics. - In
WHEREclauses, compare the column directly to the computed cutoff for better clarity and indexing. - Be explicit about whether your application means 30 calendar days or the last 30 times 24 hours.
Related reading
- How to switch databases in psql?
- How to switch Keyspace in Cassandra using CQL?
- How to synchronize data between two tables in different databases
- How to synchronize distributed system data across cassandra clusters
- how to take a keyspace as a dump in cassandra?
- How to take a merge replication back up?
- How to take backup of a single table in a MySQL database?
- How to test an SQL Update statement before running it?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.