Adding And Subtracting Dates in PHP

Here are some examples by which you can add or subtract date. e.g these simple codes can find the 2 week before or after date.

Subtracting Years from a date

$date = “1988-08-01”;

$newdate = strtotime ( ‘-3 year’ , strtotime ( $date ) ) ;

$newdate = date ( ‘Y-m-j’ , $newdate );

echo $newdate;

Subtracting Months from a date

$date = “1988-08-01”;

$newdate = strtotime ( ‘-3 month’ , strtotime ( $date ) ) ;

$newdate = date ( ‘Y-m-j’ , $newdate );

echo $newdate;

Subtracting Weeks from a date

$date = “1988-08-01”;

$newdate = strtotime ( ‘-3 week’ , strtotime ( $date ) ) ;

$newdate = date ( ‘Y-m-j’ , $newdate );

echo $newdate;

Subtracting days from a date

$date = “1988-08-01”;

$newdate = strtotime ( ‘-3 day’ , strtotime ( $date ) ) ;

$newdate = date ( ‘Y-m-j’ , $newdate );

echo $newdate;

Adding Years, Month, Week and Days in a date

For adding year, month, week and date you have to only change Plus sign in place of Negative sign.

e.g

$date = “1988-08-01”;

$newdate = strtotime ( ‘+3 year’ , strtotime ( $date ) ) ;

$newdate = date ( ‘Y-m-j’ , $newdate );

echo $newdate;

OR

$date = “1988-08-01”;

$newdate = strtotime ( ‘3 year’ , strtotime ( $date ) ) ;

$newdate = date ( ‘Y-m-j’ , $newdate );

echo $newdate;

Bansal Sumit

To seek challenging roles and to achieve excellence in the field of Open Source Web Technology with a profound spirit of commitment and dedication.

You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.