

Maruf scribbled this post.
Here’s how you add and subtract dates from one another. For example, this simple code can calculate what the date will be 2 weeks before or after 1998-08-14 (yyyy-mm-dd).
Subtracting days from a date
The following example will subtract 3 days from 1998-08-14. The result will be 1998-08-11.
1 2 3 4 5 | $date = "1998-08-14"; $newdate = strtotime ( '-3 day' , strtotime ( $date ) ) ; $newdate = date ( 'Y-m-j' , $newdate ); echo $newdate; |
Subtracting Weeks from a date
The following example will subtract 3 weeks from 1998-08-14. The result will be 1998-07-24. Notice that the only difference in the code is the week statement.
1 2 3 4 5 | $date = "1998-08-14"; $newdate = strtotime ( '-3 week' , strtotime ( $date ) ) ; $newdate = date ( 'Y-m-j' , $newdate ); echo $newdate; |
Subtracting Months from a date
The following example will subtract 3 months from 1998-08-14. The result will be 1998-05-14. Notice that the only difference in the code is the month statement.
1 2 3 4 5 | $date = "1998-08-14"; $newdate = strtotime ( '-3 month' , strtotime ( $date ) ) ; $newdate = date ( 'Y-m-j' , $newdate ); echo $newdate; |
Subtracting Years from a date
The following example will subtract 3 years from 1998-08-14. The result will be 1995-08-14. Notice that the only difference in the code is the year statement.
1 2 3 4 5 | $date = "1998-08-14"; $newdate = strtotime ( '-3 year' , strtotime ( $date ) ) ; $newdate = date ( 'Y-m-j' , $newdate ); echo $newdate; |
Adding days, months, weeks and years from a date
There isn’t really much difference from subtracting and adding dates. To add dates, just use any of the examples above and replace the negative (-) with a positive (+) e.g. ‘+3 weeks’











comments
You could also just do:
echo date(“Y-m-j”, strtotime(“1998-08-14 -3 days”));
(sub days, weeks, months, as needed)
Thanks from Turkey…
HI,Thanx for the hint,but what about subtracting a date from another date.e.g check in date from check out date to get the number of days to spend.Thanx once more
Man! This is great, thank you so much.It helps a lot.
Thank you! Exactly what I was looking for!
Thank you! it’s good.
i got correct coding…it’s useful for me…
Hey Guys , thanks for this very helpful, but how can I add $variable days?
inteh above example we – 3 days.
say i need to add a $varible numbe rof days ? how can i do that ? anybody ?
cheers
Thanks!
Thanks this is exactly what I was looking for.
Hey thanks Maruf. Lovely piece of code!
hi, i’m having problem with this function.
it works fine until when i tried to add 40 year to the date, the result turns out to be “1970-01-1″.
It’s weird, coz it works fine with +39 year, but not +40 & above. has anyone face the same problem?