Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the day after tomorrow in PHP using DateTime?

I have an Eloquent query that currently looks like this:

$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.

like image 675
Pat841 Avatar asked Aug 14 '13 17:08

Pat841


People also ask

How can I get tomorrow day in php?

$newDate = date('Y-m-d', strtotime('tomorrow')); echo $newDate; ?>

How can I get the day of a specific date with php?

You can use the date function. I'm using strtotime to get the timestamp to that day ; there are other solutions, like mktime , for instance.

How can I get yesterday date in php?

Using time() to Get Yesterday's Date in PHP The time() function returns the current timestamp. If we subtract its value, then we get the timestamp of the same time yesterday.

What is Strtotime php?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


3 Answers

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'

like image 95
hek2mgl Avatar answered Oct 23 '22 08:10

hek2mgl


The day after tomorrow is two days from now, so this will work:-

$dayAfterTomorrow = (new \DateTime())->add(new \DateInterval('P2D'));

See it working

like image 45
vascowhite Avatar answered Oct 23 '22 07:10

vascowhite


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'));

gives you exactly 2 days from now and hours and minutes are kept

like image 4
Julius Koronci Avatar answered Oct 23 '22 08:10

Julius Koronci