在 PHP 中为日期添加天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8085886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:56:20  来源:igfitidea点击:

Add days to a date in PHP

phpfunctiondate

提问by user1038814

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

是否有任何可用的 php 函数,我可以在其中添加天数以组成另一个日期?例如,我有以下格式的日期:27-December-2011

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

如果我在上面加上 7,它应该给出:03-January-2012。

Many thanks

非常感谢

回答by Fabrizio

Try this

尝试这个

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

回答by Simone

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");

回答by Aurelio De Rosa

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

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

回答by Smamatti

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

回答by Lucian Minea

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 ;).

当然,您可以使用变量代替字符串。如果回答问题的人不会使事情复杂化,那就太好了。更少的代码,意味着更少的时间浪费在服务器上;)。

回答by Matthew

$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()).

将格式字符串更改为您想要的任何内容。(请参阅 的文档date())。

回答by Wasim Khan

$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;