php 向日期添加天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2332681/
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
Add number of days to a date
提问by Pankaj Khurana
I want to add number of days to current date: I am using following code:
我想将天数添加到当前日期:我正在使用以下代码:
$i=30;
echo $date = strtotime(date("Y-m-d", strtotime($date)) . " +".$i."days");
But instead of getting proper date i am getting this: 2592000
但我得到的不是正确的日期: 2592000
Please suggest.
请建议。
回答by Gordon
This should be
这应该是
echo date('Y-m-d', strtotime("+30 days"));
strtotime
strtotime
expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
期望得到一个包含美国英语日期格式的字符串,并将尝试将该格式解析为 Unix 时间戳(自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数),相对于 now 中给出的时间戳,或如果没有提供现在,则为当前时间。
while date
尽管 date
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
返回根据给定格式字符串格式化的字符串,使用给定的整数时间戳或当前时间(如果没有给定时间戳)。
See the manual pages for
参见手册页
and their function signatures.
以及它们的函数签名。
回答by Gowri
This one might be good
这个可能不错
function addDayswithdate($date,$days){
$date = strtotime("+".$days." days", strtotime($date));
return date("Y-m-d", $date);
}
回答by chx
$date = new DateTime();
$date->modify('+1 week');
print $date->format('Y-m-d H:i:s');
or print date('Y-m-d H:i:s', mktime(date("H"), date("i"), date("s"), date("m"), date("d") + 7, date("Y"));
或者 print date('Y-m-d H:i:s', mktime(date("H"), date("i"), date("s"), date("m"), date("d") + 7, date("Y"));
回答by shraddha Dharmamer
$today=date('d-m-Y');
$next_date= date('d-m-Y', strtotime($today. ' + 90 days'));
echo $next_date;
回答by Andy
You can add like this as well, if you want the date 5 days from a specific date :
如果您想要从特定日期开始 5 天的日期,您也可以这样添加:
You have a variable with a date like this (gotten from an input or DB or just hard coded):
您有一个带有这样日期的变量(从输入或数据库中获取或只是硬编码):
$today = "2015-06-15"; // Or can put $today = date ("Y-m-d");
$fiveDays = date ("Y-m-d", strtotime ($today ."+5 days"));
echo $fiveDays; // Will output 2015-06-20
回答by Insectatorious
You could also try:
你也可以试试:
$date->modify("+30 days");
$date->modify("+30 days");
回答by Bjoern
Keep in mind, the change of clock changes because of daylight saving time might give you some problems when only calculating the days.
请记住,由于夏令时而导致的时钟变化可能会在仅计算天数时给您带来一些问题。
Here's a little php function which takes care of that:
这是一个处理这个的小 php 函数:
function add_days($date, $days) {
$timeStamp = strtotime(date('Y-m-d',$date));
$timeStamp+= 24 * 60 * 60 * $days;
// ...clock change....
if (date("I",$timeStamp) != date("I",$date)) {
if (date("I",$date)=="1") {
// summer to winter, add an hour
$timeStamp+= 60 * 60;
} else {
// summer to winter, deduct an hour
$timeStamp-= 60 * 60;
} // if
} // if
$cur_dat = mktime(0, 0, 0,
date("n", $timeStamp),
date("j", $timeStamp),
date("Y", $timeStamp)
);
return $cur_dat;
}
回答by Paul Truesdell
You can do it by manipulating the timecode or by using strtotime(). Here's an example using strtotime.
您可以通过操作时间码或使用strtotime() 来实现。这是一个使用 strtotime 的示例。
$data['created'] = date('Y-m-d H:i:s', strtotime("+1 week"));
$data['created'] = date('Ymd H:i:s', strtotime("+1 周"));
回答by Laxman13
You can use strtotime()$data['created'] = date('Y-m-d H:m:s', strtotime('+1 week'));
您可以使用 strtotime()$data['created'] = date('Y-m-d H:m:s', strtotime('+1 week'));
回答by Fabrizio
I know this is an old question, but for PHP <5.3 you could try this:
我知道这是一个老问题,但对于 PHP <5.3 你可以试试这个:
$date = '05/07/2013';
$add_days = 7;
$date = date('Y-m-d',strtotime($date) + (24*3600*$add_days)); //my preferred method
//or
$date = date('Y-m-d',strtotime($date.' +'.$add_days.' days');

