laravel 如何使用DateTime在PHP中获得后天?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18238394/
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
How to get the day after tomorrow in PHP using DateTime?
提问by Pat841
I have an Eloquent query that currently looks like this:
我有一个 Eloquent 查询,目前看起来像这样:
$rides = Ride::where('date', '>=', new \DateTime('today'))
->where('date', '<=', new \DateTime('tomorrow'))
->get();
Which works fine, my question is, how do I go about formatting it like so:
哪个工作正常,我的问题是,我如何像这样格式化它:
$rides = Ride::where('date', '>=', new \DateTime('tomorrow'))
->where('date', '<=', new \DateTime('tomorrow + one'))
->get();
Meaning I am trying to find the results whose dates are between tomorrow and the day after tomorrow. Any help would be greatly appreciated.
这意味着我试图找到日期在明天和后天之间的结果。任何帮助将不胜感激。
回答by hek2mgl
If you want to get the day after tomorrow you can use:
如果你想得到后天你可以使用:
new \DateTime('tomorrow + 1day')
You can find more information in the manual page 'Relative time formats'
您可以在手册页“相对时间格式”中找到更多信息
回答by Julius Koronci
new \DateTime('tomorrow + 1day')
new \DateTime('tomorrow + 1day')
It's OK if you are not interested in hours and minutes.. It's gives you always midnigth but
如果你对小时和分钟不感兴趣也没关系。它总是给你午夜但
$dayAfterTomorrow = (new \DateTime())->add(new \DateInterval('P2D'));
$dayAfterTomorrow = (new \DateTime())->add(new \DateInterval('P2D'));
gives you exactly 2 days from now and hours and minutes are kept
为您提供从现在开始的 2 天,并保留小时和分钟
回答by vascowhite
The day after tomorrow is two days from now, so this will work:-
后天是从现在起两天后,所以这将起作用:-
$dayAfterTomorrow = (new \DateTime())->add(new \DateInterval('P2D'));