PHP- Adding And Subtracting Dates

PHP Scripts, Tips & Tricks

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.

$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.

$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.

$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.

$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’


  1. veddermatic spoke on 18/02/2009

    You could also just do:

    echo date(“Y-m-j”, strtotime(“1998-08-14 -3 days”));

    (sub days, weeks, months, as needed)

  2. Kerem spoke on 17/06/2009

    Thanks from Turkey…

  3. Shepherd spoke on 24/08/2009

    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

  4. 1mr3yn spoke on 04/09/2009

    Man! This is great, thank you so much.It helps a lot.

  5. Darcy spoke on 13/09/2009

    Thank you! Exactly what I was looking for!

  6. Vivek spoke on 21/10/2009

    Thank you! it’s good.
    i got correct coding…it’s useful for me…

  7. Afzal spoke on 23/11/2009

    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 ?


  8. M spoke on 28/01/2010


  9. Jefffan24 spoke on 19/02/2010

    Thanks this is exactly what I was looking for.

  10. Rachna spoke on 02/03/2010

    Hey thanks Maruf. Lovely piece of code!

  11. zurin spoke on 08/03/2010

    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?

  12. chill spoke on 09/03/2010

    hi what can i do for variable days?

  13. Hiram spoke on 21/03/2010

    Excelente script, gracias!

  14. xantov spoke on 06/04/2010

    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

  15. Ash spoke on 20/05/2010

    chill, here is how you can add substract days as variables:

    $diff = 16;

    $newdate = strtotime( ‘+’ . $diff .’ day’ , strtotime ( $date1 ) ) ;

  16. Vincent spoke on 11/06/2010

    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?

  17. Diezdedos spoke on 25/06/2010

    Muchas gracias!!!

  18. Legalize Marijuana spoke on 08/07/2010

    It works! 🙂

  19. andy spoke on 25/07/2010

    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.

  20. Lynn spoke on 09/08/2010

    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.

  21. sheraz spoke on 30/09/2010

    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.

  22. suressh spoke on 23/10/2010

    Cool..Works Fine

  23. Ravinder spoke on 06/12/2010

    can we add more than 50 years to a date

  24. Ian spoke on 16/12/2010

    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


  25. dian spoke on 16/12/2010

    great, its very simple. usually i am using mktime for adding days, month etc. thank you 🙂

  26. Yves spoke on 08/03/2011

    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?



  27. Yves spoke on 08/03/2011

    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!!!

  28. r1n0z33n spoke on 02/04/2011

    thnks for simple and compact tutorial…

  29. John M spoke on 13/04/2011

    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

    Can anyone help me with this

  30. Nabeel spoke on 18/04/2011

    Thanks for the tutorial. It helped a lot. 🙂

  31. pratik spoke on 13/05/2011

    i need to add 60 years but i am unable with it.
    i am just able to add 27 years only.
    plz help me

  32. saurabh spoke on 16/06/2011

    chill, here is how you can add substract days as variables:

    $diff = 16;

    $newdate = strtotime( ‘+’ . $diff .’ day’ , strtotime ( $date1 ) ) ;

  33. Ian spoke on 05/07/2011

    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???

  34. vattana spoke on 12/08/2011

    I wanna skip sat and sun and more holidays list.

    here is my code : 🙂
    $start = $month = strtotime(‘2011-08-02’);
    $end = strtotime(‘2012-08-02′);
    while($month < $end)
    $month = strtotime("+3 day", $month);//skip to mon
    echo $installment = $mydate.','.date('d/m/Y', $month), PHP_EOL."”;
    $month = strtotime(“+1 month”, $month);
    else if($mydate==’Sun’){
    $month = strtotime(“+2 day”, $month);//skip to mon
    echo $installment = $mydate.’,’.date(‘d/m/Y’, $month), PHP_EOL.””;
    $month = strtotime(“+1 month”, $month);
    $month = strtotime(“+1 month”, $month);
    But I can’t.:-) what am i wrong ?

  35. ain spoke on 30/12/2011

    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..

  36. Ndagijimanan Richard spoke on 02/02/2012

    @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

  37. leela spoke on 10/05/2012

    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?

  38. deepesh saini spoke on 24/05/2012

    great article…….It works…

  39. Brandon spoke on 06/07/2012

    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.

  40. tendai spoke on 29/07/2012

    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

  41. charles spoke on 16/08/2012

    simple & good code, nice

  42. Rendra spoke on 16/10/2012

    i love this tutorial… simple and easy learn 🙂

  43. Jack spoke on 24/12/2012

    How to subtract one date from another date, like in the above example we use – 3 weeks, suppose we have two dates, how we can subtract and get only days left?

  44. Ellis Benus - Web Design Columbia MO spoke on 18/01/2013

    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.

  45. Andy spoke on 07/02/2013

    Dude, you’re a life saver!

Leave a reply

Your email address will not be published. Required fields are marked *