Carbon: Upcoming Hour

Created on 3 Jan 2019  Â·  6Comments  Â·  Source: briannesbitt/Carbon

In attempting to get the upcoming 7am timstamp

now()->tomorrow()->addHours(7)

This works up until midnight where tomorrow becomes the next day.

Looking for a way to say now()->upcoming(‘7am’)

https://twitter.com/im_brian_d/status/1080639290500153344?s=21

macro candidate question

Most helpful comment

OK, just understood. We have a semantic problem here, midnight is not 12:00, it's 00:00, it's already the next day, so tomorrow()->hour(7) is perfectly right, it's not tow days from now, it's 1 day from now 23:59 is the day D, 00:00 is the next day.

But you can test the current hour and it's one line of code:

(Carbon::now()->hour < 7 ? Carbon::now() : Carbon::tomorrow())->hour(7)

So you can easily do your own macro:
```php
Carbon::macro('nextHour', function ($hour) {
$date = isset($this) ? $this : Carbon::now();

return ($date->hour < $hour ? $date : $date->tomorrow())->hour($hour);
});

Carbon::nextHour(7); // static way
now()->nextHour(7); // on a given Carbon instance

All 6 comments

You should better use the dedicated library to opening hours:
https://github.com/kylekatarnls/business-time

There, you can use isOpen and nextOpen to get a readable and intuitive code and mostly easy to configure day-by-day precisely.

While that package looks great, was looking for a one liner to reduce bloat and thought it might be useful to have a function like that in general, thanks anyways :)

Macro are here to turn your own business logic into a reusable method. Carbon is not responsible to handle specific business logic.

I'm sorry but your expectation is not clear (sounds different in your twitter link (either ternary or switch code) and your description, now between 7 and 20 is not "upcoming 7am"), please follow the issue pattern, with concrete examples and results.

7-20 is a specific (and easy) situation, many users would say "lunch break", week-ends, holidays, etc. that's why I made cmixin/business-time that can easily give you the upcoming 7am or now if open. So we have no reason to implement this in Carbon as it's already in cmixin/business-time that handle that precise role while Carbon doesn't.

No Im not looking for handling of between 7-20. between() handles that nicely.

Im saying carbon could assess and realize the next 7am timestamp or next Monday, or next month

Similar to nextClose & nextOpen in your package except that currently:

  • at 11:59 now()->tomorrow()->hour(7) is tomorrow at 7am
  • at 12:00 now()->tomorrow()->hour(7) is two days from now at 7am in essence

Would be awesome if:

  • at 11:59 now()->nextHour('7') is tomorrow 7am
  • at 12:00 now()->nextHour('7') is tomorrow at 7am

OK, just understood. We have a semantic problem here, midnight is not 12:00, it's 00:00, it's already the next day, so tomorrow()->hour(7) is perfectly right, it's not tow days from now, it's 1 day from now 23:59 is the day D, 00:00 is the next day.

But you can test the current hour and it's one line of code:

(Carbon::now()->hour < 7 ? Carbon::now() : Carbon::tomorrow())->hour(7)

So you can easily do your own macro:
```php
Carbon::macro('nextHour', function ($hour) {
$date = isset($this) ? $this : Carbon::now();

return ($date->hour < $hour ? $date : $date->tomorrow())->hour($hour);
});

Carbon::nextHour(7); // static way
now()->nextHour(7); // on a given Carbon instance

@kylekatarnls thank you! Much appreciated. Sorry for the confusion 😅

Was this page helpful?
0 / 5 - 0 ratings