简体   繁体   English

在PHP中为日期添加天数

[英]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? 有没有可用的php函数,我可以在其中添加几天以组成另一个日期? For example, I have a date in the following format: 27-December-2011 例如,我的日期格式如下:2011年12月27日

If I add 7 to the above, it should give: 03-January-2012. 如果我在上面加上7,则应为:2012年1月3日。

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. 您可以使用DateTime的添加方法。 Anyway this solution works for php version >= 5.3 无论如何,此解决方案适用于PHP版本> = 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() ). (请参阅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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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