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

feel free to leave a scribble

Name:
Email:
gravatar
What an image next to your comments?
visit gravatar.com
Message:
want a website, do ya?

latest projects

Image frame
RG Active
view site- RG Active
Image frame
Property Investment Project
view site- Property Investment Project

latest scribbles

latest comments

zurin Gravatar frame
zurin said
hi, i’m having problem with this function. it works fine...
Karlos Gravatar frame
Karlos said
Awesome awesome awesome :) Just started re-designing...
satheesh Gravatar frame
satheesh said
It solved my problem. Thank you. Is it possible to get the...

blog categories

Get a free quote