在PHP中添加日期到日期

use*_*814 13 php date function

有没有任何PHP功能,我可以添加日期来弥补另一个日期?例如,我的日期格式如下:2011年12月27日

如果我在上面添加7,它应该给出:2012年1月3日.

非常感谢

Fab*_*zio 16

试试这个

$add_days = 7;
$date = date('Y-m-d',strtotime($date) + (24*3600*$add_days));
Run Code Online (Sandbox Code Playgroud)

  • 也许你的意思是'约会('Ym-d',strtotime($ date.'+'.$ add_days.'days'))`.也应该这样做 (2认同)

Sim*_*one 9

看看这个简单的片段

$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");
Run Code Online (Sandbox Code Playgroud)

  • 这完全是错误的.这样,它返回一个时间整数.需要移除最外面的strtotime并且在内部strtotime函数内移动"+ x天".例如:$ date = date("Ymd",strtotime($ date."+ 30天")); (3认同)

Aur*_*osa 5

您可以使用DateTime 的add方法.无论如何,这个解决方案适用于php版本> = 5.3


Sma*_*tti 5

date('Y-m-d', strtotime('+6 days', strtotime($original_date)));
Run Code Online (Sandbox Code Playgroud)