Thursday, February 16, 2012

Adding and substracting date in PHP

In this time I want to share code for letting you adding and subtracting date in php. You can use this for easy to add the day, week, or year from specific date in PHP.

Let's take a look for this code :

$date = "2012-02-16";
$newdate = strtotime ( '-3 day' , strtotime ( $date ) ) ; //substracting date to 3 days before
$newdate2 = strtotime ( '+3 week' , strtotime ( $date ) ) ;/substracting date to 3 weeks after
 
$newdate3 = strtotime ( '-3 year' , strtotime ( $date ) ) ;//substracting date to 3 years before
$newdate = date ( 'Y-m-j' , $newdate ); //save to new variable
echo $newdate;

The other code for you to use the date variable in PHP

$d = mktime(0,0,0,$month,$day,$year);
$end_date = date("Y m d",strtotime("+2 days",$d));
//To add days to current date
date('Y-m-d', strtotime("+2 days"));


Just share the code in my mind.

No comments:

Post a Comment