PHP- Adding And Subtracting Dates

Image frame
PHP- Adding And Subtracting Dates
Maruf
Jan 6th, 2009
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’


Filed away: PHP

comments

Image frame
1

You could also just do:

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

(sub days, weeks, months, as needed)

veddermatic
Feb 18th, 2009
Image frame
2

Thanks from Turkey…

Kerem
Jun 17th, 2009
Image frame
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

Shepherd
Aug 24th, 2009
Image frame
4

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

1mr3yn
Sep 4th, 2009
Image frame
5

Thank you! Exactly what I was looking for!

Darcy
Sep 13th, 2009
Image frame
6

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

Vivek
Oct 21st, 2009
Image frame
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

Afzal
Nov 23rd, 2009
Image frame
8

Thanks!

M
Jan 28th, 2010
Image frame
9

Thanks this is exactly what I was looking for.

Jefffan24
Feb 19th, 2010
Image frame
10

Hey thanks Maruf. Lovely piece of code!

Rachna
Mar 2nd, 2010
Image frame
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?

zurin
Mar 8th, 2010
Image frame
12

hi what can i do for variable days?

chill
Mar 9th, 2010
Image frame
13

Excelente script, gracias!

Hiram
Mar 21st, 2010
Image frame
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

xantov
Apr 6th, 2010
Image frame
15

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

$diff = 16;

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

Ash
May 20th, 2010
Image frame
16

Muchas gracias!!!

Diezdedos
Jun 25th, 2010
Image frame
17

It works! :)

Legalize Marijuana
Jul 8th, 2010
Image frame
18

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.

andy
Jul 25th, 2010
Image frame
19

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.

Lynn
Aug 9th, 2010

feel free to leave a scribble

Name:
Email:
gravatar
Want an image next to your comments?
visit gravatar.com
Message:
Get a free quote