Databases 6 min read

MySQL Date and Time Functions: addtime, adddate, date_add, subtime, subdate, date_sub

This article explains MySQL's date and time manipulation functions—including addtime, adddate, date_add, subtime, subdate, and date_sub—showing their syntax, usage with numeric and INTERVAL arguments, and providing example queries for adding or subtracting seconds, minutes, days, weeks, months, and years.

php Courses
php Courses
php Courses
MySQL Date and Time Functions: addtime, adddate, date_add, subtime, subdate, date_sub

MySQL provides several functions to manipulate date and time values.

1. addtime() – adds a specified number of seconds to a date/time. select addtime(now(),1); -- add 1 second 2. adddate() – adds days or an INTERVAL to a date.

select adddate(now(),1); -- add 1 day</code>
<code>select adddate(now(), interval 1 day); -- add 1 day</code>
<code>select adddate(now(), interval 1 hour); -- add 1 hour</code>
<code>select adddate(now(), interval 1 minute); -- add 1 minute</code>
<code>select adddate(now(), interval 1 second); -- add 1 second</code>
<code>select adddate(now(), interval 1 microsecond); -- add 1 microsecond</code>
<code>select adddate(now(), interval 1 week); -- add 1 week</code>
<code>select adddate(now(), interval 1 month); -- add 1 month</code>
<code>select adddate(now(), interval 1 quarter); -- add 1 quarter</code>
<code>select adddate(now(), interval 1 year); -- add 1 year

3. date_add() – adds an INTERVAL to a date, similar to adddate().

select date_add(now(), interval 1 day); -- add 1 day</code>
<code>select date_add(now(), interval 1 hour); -- add 1 hour</code>
<code>select date_add(now(), interval 1 minute); -- add 1 minute</code>
<code>select date_add(now(), interval 1 second); -- add 1 second</code>
<code>select date_add(now(), interval 1 microsecond); -- add 1 microsecond</code>
<code>select date_add(now(), interval 1 week); -- add 1 week</code>
<code>select date_add(now(), interval 1 month); -- add 1 month</code>
<code>select date_add(now(), interval 1 quarter); -- add 1 quarter</code>
<code>select date_add(now(), interval 1 year); -- add 1 year

4. subtime() – subtracts a specified number of seconds from a date/time. select subtime(now(),1); -- subtract 1 second 5. subdate() – subtracts days or an INTERVAL from a date, mirroring adddate().

select subdate(now(),1); -- subtract 1 day</code>
<code>select subdate(now(), interval 1 day); -- subtract 1 day</code>
<code>select subdate(now(), interval 1 hour); -- subtract 1 hour</code>
<code>select subdate(now(), interval 1 minute); -- subtract 1 minute</code>
<code>select subdate(now(), interval 1 second); -- subtract 1 second</code>
<code>select subdate(now(), interval 1 microsecond); -- subtract 1 microsecond</code>
<code>select subdate(now(), interval 1 week); -- subtract 1 week</code>
<code>select subdate(now(), interval 1 month); -- subtract 1 month</code>
<code>select subdate(now(), interval 1 quarter); -- subtract 1 quarter</code>
<code>select subdate(now(), interval 1 year); -- subtract 1 year

6. date_sub() – subtracts an INTERVAL from a date, analogous to date_add().

select date_sub(now(), interval 1 day); -- subtract 1 day</code>
<code>select date_sub(now(), interval 1 hour); -- subtract 1 hour</code>
<code>select date_sub(now(), interval 1 minute); -- subtract 1 minute</code>
<code>select date_sub(now(), interval 1 second); -- subtract 1 second</code>
<code>select date_sub(now(), interval 1 microsecond); -- subtract 1 microsecond</code>
<code>select date_sub(now(), interval 1 week); -- subtract 1 week</code>
<code>select date_sub(now(), interval 1 month); -- subtract 1 month</code>
<code>select date_sub(now(), interval 1 quarter); -- subtract 1 quarter</code>
<code>select date_sub(now(), interval 1 year); -- subtract 1 year
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

sqlmysqlDate Functionsadddateaddtimedate_addsubdate
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.