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.
$newDate = date('Y-m-d', strtotime('tomorrow')); echo $newDate; ?>
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.
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.
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.
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'
The day after tomorrow is two days from now, so this will work:-
$dayAfterTomorrow = (new \DateTime())->add(new \DateInterval('P2D'));
See it working
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With