Adding Days to a Date with PHP

别等时光非礼了梦想. 提交于 2019-11-28 12:32:59

问题


I am trying to add a certain amount of days to a set date in PHP. However, all of the code I use is not working. Here is the code I am currently experiencing problems with:

echo date("2013-12-01", strtotime("+7 days"));

I wanting to add 7 days to the date above. When I echo out this code, it just prints '2013-12-01'. Is there a way to do this?

Thanks


回答1:


For the sake of completeness, here's how you do it with DateTime():

$datetime = new DateTime("2013-12-01");
$datetime->add(new DateInterval('P7D'));
echo $datetime->format('Y-m-d');

or

$datetime = new DateTime("2013-12-01");
$datetime->modify('+7 days');
echo $datetime->format('Y-m-d');



回答2:


You can use date_add() function:

$date = date_create('2013-12-01');
date_add($date, date_interval_create_from_date_string('7 days'));
echo date_format($date, 'Y-m-d');

This will output 2013-12-08




回答3:


It must be like this:

$NewDate = date('Y-m-d', strtotime("2013-12-01" . " +7 days"));
echo $NewDate;                                   


来源:https://stackoverflow.com/questions/17959959/adding-days-to-a-date-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!