PHP 字符串日期 + 1 天等于?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8457661/
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
PHP string date + 1 day equals?
提问by sn0ep
I have a date which is saved in a regular string.
我有一个保存在常规字符串中的日期。
// format = DD-MM-YYYY
$date = "10-12-2011";
How can I get the date-string +1 day so: 11-12-2011?
我怎样才能得到日期字符串 +1 天,所以:11-12-2011?
回答by Aaron W.
回答by mckenna
If you're trying to overwrite $date, Aaron's answer works great. But if you need the new day saved into a separate variable as I did, this works:
如果您试图覆盖 $date,Aaron 的答案会很有效。但是,如果您需要像我一样将新的一天保存到一个单独的变量中,则可以这样做:
$date = strtotime('10-12-2011'); // your date
$newDate = date('d-m-Y', strtotime("+1 day", $date)); // day after original date
回答by user5510975
if you want today +$i day
如果你今天想要 +$i day
$today = date('Y-m-d');
$tomorrow = strtotime($today." +".$i." day");
回答by Loki
You can either use the date functionto piece the date together manually (will obviously require to check for leap years, number of days in current month etc) or get strtotimeand convert what you get via the date function parsing the timestamp you got from strtotime as the second argument.
您可以使用date 函数手动将日期拼凑在一起(显然需要检查闰年,当月的天数等)或获取strtotime并通过解析从 strtotime 获得的时间戳的日期函数转换您获得的内容作为第二个论点。
回答by Dharman
You should be using DateTime
class for working with dates. Using strtotime()
might not be the best choice in the long run.
您应该使用DateTime
class 来处理日期。strtotime()
从长远来看,使用可能不是最佳选择。
To add 1 day to the data using DateTime you can use modify()
method.
要使用 DateTime 向数据添加 1 天,您可以使用modify()
方法。
$newDate = date_create_from_format('d-m-Y', $date)
->modify('+1 day')
->format('d-m-Y');