3v4l.org

run code in 300+ PHP versions simultaneously
<?php $timeslots = ["01:00","01:30","02:00","02:30","00:30","01:00","01:30","02:00","02:30","03:50", "14:00", "15:00", "15:30"]; sort($timeslots); // first we sort the array, very important $expected_time = "04:00"; $timestamp = strtotime($expected_time); $diff = PHP_INT_MAX; $index = null; foreach ($timeslots as $key => $time) { $currDiff = $timestamp - strtotime($time); // Add a condition to check if the difference was negative or not - because it'll only be the "next" hour when the difference is negative if ($currDiff < $diff && $currDiff < 0) { $index = $key; $diff = $currDiff; break; } } if(!is_null($index)) echo $timeslots[$index]; else echo 'Not found';
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 28
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 28
Branch analysis from position: 13
2 jumps found. (Code = 46) Position 1 = 21, Position 2 = 23
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 27
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 35
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 23
Branch analysis from position: 28
Branch analysis from position: 28
filename:       /in/kGdP8
function name:  (null)
number of ops:  37
compiled vars:  !0 = $timeslots, !1 = $expected_time, !2 = $timestamp, !3 = $diff, !4 = $index, !5 = $time, !6 = $key, !7 = $currDiff
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
    4     1        INIT_FCALL                                               'sort'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                                 
    6     4        ASSIGN                                                   !1, '04%3A00'
    7     5        INIT_FCALL                                               'strtotime'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $11     
          8        ASSIGN                                                   !2, $11
    8     9        ASSIGN                                                   !3, 9223372036854775807
    9    10        ASSIGN                                                   !4, null
   11    11      > FE_RESET_R                                       $15     !0, ->28
         12    > > FE_FETCH_R                                       ~16     $15, !5, ->28
         13    >   ASSIGN                                                   !6, ~16
   13    14        INIT_FCALL                                               'strtotime'
         15        SEND_VAR                                                 !5
         16        DO_ICALL                                         $18     
         17        SUB                                              ~19     !2, $18
         18        ASSIGN                                                   !7, ~19
   16    19        IS_SMALLER                                       ~21     !7, !3
         20      > JMPZ_EX                                          ~21     ~21, ->23
         21    >   IS_SMALLER                                       ~22     !7, 0
         22        BOOL                                             ~21     ~22
         23    > > JMPZ                                                     ~21, ->27
   17    24    >   ASSIGN                                                   !4, !6
   18    25        ASSIGN                                                   !3, !7
   19    26      > JMP                                                      ->28
   11    27    > > JMP                                                      ->12
         28    >   FE_FREE                                                  $15
   23    29        TYPE_CHECK                                    2  ~25     !4
         30        BOOL_NOT                                         ~26     ~25
         31      > JMPZ                                                     ~26, ->35
   24    32    >   FETCH_DIM_R                                      ~27     !0, !4
         33        ECHO                                                     ~27
   23    34      > JMP                                                      ->36
   26    35    >   ECHO                                                     'Not+found'
         36    > > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.63 ms | 1006 KiB | 15 Q