Web Design Blog

This is where we store some of our Web Development thoughts, tips and tricks, just because we like to share.

PHP- Adding And Subtracting Dates

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’

06 Jan 2009 / 36 Comments / PHP Scripts, Tips & Tricks / by Maruf

36 Comments

  1. veddermatic
    18/02/2009
    1

    You could also just do:

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

    (sub days, weeks, months, as needed)

  2. Kerem
    17/06/2009
    2

    Thanks from Turkey…

  3. Shepherd
    24/08/2009
    3

    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
    04/09/2009
    4

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

  5. Darcy
    13/09/2009
    5

    Thank you! Exactly what I was looking for!

  6. Vivek
    21/10/2009
    6

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

  7. Afzal
    23/11/2009
    7

    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

  8. M
    28/01/2010
    8

    Thanks!

  9. Jefffan24
    19/02/2010
    9

    Thanks this is exactly what I was looking for.

  10. Rachna
    02/03/2010
    10

    Hey thanks Maruf. Lovely piece of code!

  11. zurin
    08/03/2010
    11

    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
    09/03/2010
    12

    hi what can i do for variable days?

  13. Hiram
    21/03/2010
    13

    Excelente script, gracias!

  14. xantov
    06/04/2010
    14

    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
    20/05/2010
    15

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

    $diff = 16;

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

  16. Vincent
    11/06/2010
    16

    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
    25/06/2010
    17

    Muchas gracias!!!

  18. Legalize Marijuana
    08/07/2010
    18

    It works! :)

  19. andy
    25/07/2010
    19

    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
    09/08/2010
    20

    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
    30/09/2010
    21

    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
    23/10/2010
    22

    Cool..Works Fine

  23. Ravinder
    06/12/2010
    23

    can we add more than 50 years to a date

  24. Ian
    16/12/2010
    24

    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.

  25. 16/12/2010
    25

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

  26. 08/03/2011
    26

    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.

  27. 08/03/2011
    27

    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. 02/04/2011
    28

    thnks for simple and compact tutorial…

  29. 13/04/2011
    29

    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

  30. 18/04/2011
    30

    Thanks for the tutorial. It helped a lot. :)

  31. 13/05/2011
    31

    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. 16/06/2011
    32

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

    $diff = 16;

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

  33. Ian
    05/07/2011
    33

    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. ain
    30/12/2011
    34

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

  35. 35

    @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

  36. leela
    10/05/2012
    36

    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?

Leave a Reply

© 2012 BrightCherry :)