简体   繁体   中英

Add days to a date in PHP

Is there any php function available where I can add days to a date to make up another date? For example, I have a date in the following format: 27-December-2011

If I add 7 to the above, it should give: 03-January-2012.

Many thanks

Try this

$add_days = 7;
$date = date('Y-m-d',strtotime($date) + (24*3600*$add_days));

Look at this simple snippet

$date = date("Y-m-d");// current date

$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");

You can use the add method of DateTime. Anyway this solution works for php version >= 5.3

date('Y-m-d', strtotime('+6 days', strtotime($original_date)));

Actually it's easier than all that.

$some_var = date("Y-m-d",strtotime("+7 day"))

You can use a variable instead of the string, of course. It will be great if the people answering the questions, won't complicate things. Less code, means less time to waste on the server ;).

$date = new DateTime('27-December-2011');
$date->add(new DateInterval('P7D'));
echo $date->format('d-F-Y') . "\n";

Change the format string to be whatever you want. (See the documentation for date() ).

$registered = $udata->user_registered;
$registered = date( "d m Y", strtotime( $registered ));
$challanexpiry = explode(' ', $registered);
$day   = $challanexpiry[0];
$month = $challanexpiry[1];
$year  = $challanexpiry[2];
$day = $day+10;
$bankchallanexpiry = $day . " " . $month . " " . $year;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM