skip to Main Content

in a laravel app I have a range of times(start and end), if the end time is after 00:00:00, then it would be set to the next Day

edit

for exemple :
start = 22:00:00 and end = 04:00:00 so here end hour is after 00:00:00 so it should be set to the next day

2

Answers


  1. Use $carbon->gt().

    if ($startDate->gt(Carbon::today()->startOfDay()) {
        ...
    }
    
    Login or Signup to reply.
  2. Usually if start time and end time is on the same day, $startTime should be less then $endTime, right?
    If not so, then $endTime they must to be happening different days

    So you can just write:

    $startTime = CarbonCarbon::parse('22:00:00');
    $endTime = CarbonCarbon::parse('04:00:00');
    
    if($startTime->gt($endTime)) {
       // Here you go, add next day or do whatever you want to do      
    }
    

    This approach doesn’t work and gives you incorrect result if difference between $startTime and $endTime is more than 24 hours. (For example: $startTime = '22:00:00' and $endTime = '23:00:00' but $endTime is on the next day. In this cas you must have dates present, otherwise you can’t determine if thery are hapening on same day or not )

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search