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’
This blog post was written on 06 Jan 2009 by Maruf, and has 43 Comments so far
Learning & sharing
This is where we store some of our Web Development thoughts, tips and tricks for both our own uses and for others to learn from. Sharing is caring.
Please feel free to contrinute to our blog posts; perhaps even teach us a few tricks of your own. We'd love to hear your thoughts.
Social
Feel free to share this blog post and/or subscribe to our RSS feed :)
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?
hi what can i do for variable days?
Excelente script, gracias!
i have problem when substract date to februari month.
$date = “2010-05-30″;
$newdate = strtotime ( ‘-3 month’ , strtotime ( $date ) ) ;
$newdate = date ( ‘Y-m-j’ , $newdate );
echo $newdate;
The codes gives result 2010-03-2, the correct result should be 2010-02-28
can you fix it and mail me the fix? thks
chill, here is how you can add substract days as variables:
$diff = 16;
$newdate = strtotime( ‘+’ . $diff .’ day’ , strtotime ( $date1 ) ) ;
Unfortunately, this is unreliable.
If I enter 2010-06-31 and subtract three months using this method, it returns 2010-04-1.
Does anybody know if this is a fixable problem?
Muchas gracias!!!
It works!
Hey, the reson that your having trouble with adding 40 and not 39 to the year is in the way that php codes 2 digit years. just change your code into a 4 digit format and that will fix it for you.
No. The reason you are having trouble adding 40 years is because of the limitation of the unix timestamp. It can’t show dates beyond 2038, until they fix this, I don’t know of a workaround.
hi i have used this script. i am facing problem when i upload the file to the server. this script is working correctly at localhost but after uploading it does not subtract the dates and shows same dates.
can you guide me what is the problem.
Cool..Works Fine
can we add more than 50 years to a date
Hey great article helped me lots
I could actually understand how this code worked unlike the examples that I looked at from other websites.
Thanks again
Ian.
great, its very simple. usually i am using mktime for adding days, month etc. thank you
Hi, I have a problem that is similar to Xantov when substracting months to a date.
I am doing the following in PHP version 5.2.9-2:
$date_piece=new DateTime(“2011-03-30″); // March 30th 2011
// Substract one month
$date_piece->modify(“-1 month”);
echo $date_piece->format(‘Y-m-d’);
The result is that it displays 2011-03-02 (March 2nd 2011). I would like to get February 28th 2011 instead. Anybody has a workaround for me?
Thx,
Yves.
Actually I found a work around: I am using the sql function DATE_SUB instead of attempting to perform the time interval substraction in PHP. Then March 30th 2011 minus one month is Feb 28th 2011, which is what I wanted. I’m lucky that I use an SQL database!!!
thnks for simple and compact tutorial…
I need to use a variable tom replace the date string in this example.
Here is my recordset.
mysql_select_db($database_Alameda, $Alameda);
$query_Recordset1 = “SELECT * FROM AL_eventDate LIMIT 1″;
$Recordset1 = mysql_query($query_Recordset1, $Alameda) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
I want to replace the $date = “1998-08-14″;
with a result from the DB like this but i am getting
$date = $Recordset1;
$date = “1998-08-14″;
$newdate = strtotime ( ‘-3 day’ , strtotime ( $date ) ) ;
$newdate = date ( ‘Y-m-j’ , $newdate );
echo $newdate;
So I did this
$date = $Recordset1;
$newdate = strtotime ( ‘-3 day’ , strtotime ( $Recordset1 ) ) ;
$newdate = date ( ‘M-d-Y’ , $newdate );
echo $newdate;
I am getting this using the above code change.
Warning: strtotime() expects parameter 1 to be string, resource given in /home2/alamedap/public_html/index.php on line 47
Dec-28-1969
Can anyone help me with this
Thanks for the tutorial. It helped a lot.
i need to add 60 years but i am unable with it.
i am just able to add 27 years only.
plz help me
chill, here is how you can add substract days as variables:
$diff = 16;
$newdate = strtotime( ‘+’ . $diff .’ day’ , strtotime ( $date1 ) ) ;
This looks great but how do I get a certain number of days e.g. -3 days to subtract from a variable date that is selected by the user in a drop-down calendar, on a previous page???
Hi, I need help..
i’ve date from database in format : 19/11/2009…i need to adding 2 weeks from this date
$date = “1/11/2009″;
so, here i need to convert it first into english format right?
$date = date ( ‘Y-m-d’ , $date );
$newdate = strtotime ( ‘+2 week’ , strtotime ( $date ) ) ;
$newdate = date ( ‘d/m/Y’ , $newdate);
The codes gives result 15/01/1970, the correct result should be 21/11/2011
Can anyone help me with this..thanks..
@aim Why cant you use
$date = date ( ‘Y-m-d’ , $date );
$newdate = strtotime ( ‘+14 day’ , strtotime ( $date ) ) ;
$newdate = date ( ‘d/m/Y’ , $newdate);
Tell me if it works for you
i find the 12 week after the user given date,and i stored that date in a variable,now i want to add 1 week to the 12 week date variable,i wrote the code but it shows date as “1970-01-08″.how can i write the exact code?
great article…….It works…
I needed to subtract 60 days from a MYSQL DATETIME field in a query. This got me there:
$mysqldatetime = date(‘Y-m-d H:i:s’);
$sixtydaysago = strtotime (‘-60 day’, strtotime($mysqldatetime));
$sixtydaysago = date (‘Y-m-d H:i:s’, $sixtydayago);
These lines can probably be combined but… It works and I’m done messing with it. Thanks.
How do I subtract and/or add hours to a date in the format yyyy-mm-dd hh:mm:ss?
ie how do I add 16:49:13 to 2012-06-18 22:03:18 ?
and also, how do I add 16:49:13 to 2012-06-19 14:52:31?
I need the result in the format hh:mm:ss
Thanks in advance
simple & good code, nice
i love this tutorial… simple and easy learn
thanks
Many thanks. This is exactly what I was looking for. Especially thank you for breaking out the explanations for adding all the different times: days, weeks, etc… Really useful. Thanks again.
Dude, you’re a life saver!