Pat841
Pat841

Reputation: 2953

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.

Upvotes: 11

Views: 19625

Answers (3)

Julius Koronci
Julius Koronci

Reputation: 417

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

Upvotes: 4

vascowhite
vascowhite

Reputation: 18440

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

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

See it working

Upvotes: 2

hek2mgl
hek2mgl

Reputation: 157947

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'

Upvotes: 35

Related Questions