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;


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

Share This Post

Related Articles

© 2017 Sumit Bansal. All rights reserved. Site Admin · Entries RSS · Comments RSS
Powered by WordPress · Designed by Theme Junkie