php 将日期添加到当前日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3918646/
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 day to current date
提问by Brad
add a day to date, so I can store tomorrow's date in a variable.
添加日期,以便我可以将明天的日期存储在变量中。
$tomorrow = date("Y-m-d")+86400;
I forgot.
我忘了。
回答by lonesomeday
I'd encourage you to explore the PHP 5.3 DateTime
class. It makes dates and times far easier to work with:
我鼓励您探索 PHP 5.3DateTime
类。它使日期和时间更容易使用:
$tomorrow = new DateTime('tomorrow');
// e.g. echo 2010-10-13
echo $tomorrow->format('d-m-Y');
Furthermore, you can use the + 1 day
syntax with any date:
此外,您可以将+ 1 day
语法用于任何日期:
$xmasDay = new DateTime('2010-12-24 + 1 day');
echo $xmasDay->format('Y-m-d'); // 2010-12-25
回答by casablanca
date
returns a string, whereas you want to be adding 86400 seconds to the timestamp. I think you're looking for this:
date
返回一个字符串,而您希望将 86400 秒添加到时间戳。我想你正在寻找这个:
$tomorrow = date("Y-m-d", time() + 86400);
回答by Matchu
date()
returns a string, so adding an integer to it is no good.
date()
返回一个字符串,因此向它添加一个整数是不好的。
First build your tomorrow timestamp, using strtotime
to be not only clean but more accurate (see Pekka's comment):
首先建立你明天的时间戳,使用strtotime
不仅干净而且更准确(见 Pekka 的评论):
$tomorrow_timestamp = strtotime("+ 1 day");
Then, use it as the second argument for your date
call:
然后,将其用作date
调用的第二个参数:
$tomorrow_date = date("Y-m-d", $tomorrow_timestamp);
Or, if you're in a super-compact mood, that can all be pushed down into
或者,如果你的心情非常紧凑,那么这一切都可以推到
$tomorrow = date("Y-m-d", strtotime("+ 1 day"));
回答by David Snabel-Caunt
Nice and obvious:
漂亮而明显:
$tomorrow = strtotime('tomorrow');
回答by Koushik Das
You can use the add
method datetime
class.
Eg, you want to add one day to current date and time.
您可以使用add
方法datetime
类。例如,您想在当前日期和时间上增加一天。
$today = new DateTime();
$today->add(new DateInterval('P1D'));
Further reference php datetime add
进一步参考php datetime add
Hope this helps.
希望这可以帮助。
回答by Mat Barnett
I find mktime()
most useful for this sort of thing. E.g.:
我发现mktime()
对这类事情最有用。例如:
$tomorrow=date("Y-m-d", mktime(0, 0, 0, date("m"), date("d")+1, date("Y")));