jeudi 7 mars 2019

Laravel/PHP/Carbon parse a date to one and three days prior to the hour

I'm using Laravel 5.4 and Carbon to find dates that are exactly one day and three days prior to a laravel job (to the hour) to send notifications. So for example if the date was 2019-03-10 18:00:03 or 2019-03-10 18:15:34, I'd like to send a notification tomorrow at 2019-03-07 18:00:00 and another at 2019-03-09 18:00:00. I'd like to do this in an eloquent query, but not really sure how to "lazy match" by day and hour. If I have an Appointment model with a start_date timestamp, I know I can do exact matches by day or date, but I want to find matches that disregard the hours/seconds. I was considering something along these lines:

$oneDayAppointments = Appointment::whereDay(
    'start_date',
    Carbon::now()->addDay()->day
)->get();

...would get me any appointments where the start_date is a day from now. Now how would I hone it to the day and hour. If I were to just use a where clause:

$oneDayAppointments = Appointment::where(
    'start_date',
    Carbon::now()->addHours(24)
)->get();

...I could get exact matches, but only if the minutes/seconds were a match as well. Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire