3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getTimeSlot($str) { $arr = [ "2022-09-23 08:00:00", "2022-09-23 08:30:00", "2022-09-23 09:00:00", "2022-09-23 09:30:00", "2022-09-23 10:00:00", "2022-09-23 10:30:00", "2022-09-23 11:00:00", "2022-09-23 13:00:00", "2022-09-23 13:30:00", "2022-09-23 14:00:00", "2022-09-23 14:30:00", "2022-09-23 15:00:00", "2022-09-23 16:00:00", "2022-09-23 16:30:00", "2022-09-23 17:00:00", "2022-09-23 17:30:00", "2022-09-23 17:30:00", ]; $final_datetime = "$str is not within timeslots."; $format = 'Y-m-d H:i:s'; $d = DateTime::createFromFormat($format, $str); foreach ($arr as $k => $v) { if ($v === $str) { $final_datetime = $v; break; } else { $prevDatetime = false; $nextDateTime = false; $prevStr = ""; $nextStr = ""; if (isset($arr[$k - 1])) { $prevStr = $arr[$k - 1]; } if (isset($arr[$k + 1])) { $nextStr = $arr[$k + 1]; } if ($prevStr) { $prevDatetime = DateTime::createFromFormat($format, $prevStr); } if ($nextStr) { $nextDateTime = DateTime::createFromFormat($format, $nextStr); } if ($prevDatetime && $nextDateTime && $d && ($d > $prevDatetime && $d < $nextDateTime)){ $final_datetime = $prevStr; } } } return $final_datetime; } $strings = [ "2022-09-23 07:15:00", "2022-09-23 08:00:00", "2022-09-23 08:01:00", "2022-09-23 08:29:00", "2022-09-23 08:30:00", "2022-09-23 08:31:00", "2022-09-23 09:12:00", "2022-09-23 11:29:23", "2022-09-23 15:59:00", "2022-09-23 16:29:00", "2022-09-23 16:30:00", "2022-09-23 16:31:00", "2022-09-23 16:59:00", "2022-09-23 17:00:00", "2022-09-23 17:01:00", "2022-09-23 17:29:00", "2022-09-23 17:30:00", "2022-09-23 17:31:00", "2022-09-23 18:59:00" ]; foreach ($strings as $s) { echo $s . " --> " . getTimeSlot($s) . PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 11
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/L502s
function name:  (null)
number of ops:  13
compiled vars:  !0 = $strings, !1 = $s
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   ASSIGN                                                   !0, <array>
   81     1      > FE_RESET_R                                       $3      !0, ->11
          2    > > FE_FETCH_R                                               $3, !1, ->11
   82     3    >   CONCAT                                           ~4      !1, '+--%3E+'
          4        INIT_FCALL                                               'gettimeslot'
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0  $5      
          7        CONCAT                                           ~6      ~4, $5
          8        CONCAT                                           ~7      ~6, '%0A'
          9        ECHO                                                     ~7
   81    10      > JMP                                                      ->2
         11    >   FE_FREE                                                  $3
   83    12      > RETURN                                                   1

Function gettimeslot:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 60
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 60
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 19
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 29
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 35
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 41
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 47
Branch analysis from position: 42
2 jumps found. (Code = 46) Position 1 = 48, Position 2 = 49
Branch analysis from position: 48
2 jumps found. (Code = 46) Position 1 = 50, Position 2 = 51
Branch analysis from position: 50
2 jumps found. (Code = 46) Position 1 = 52, Position 2 = 57
Branch analysis from position: 52
2 jumps found. (Code = 46) Position 1 = 54, Position 2 = 56
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 58, Position 2 = 59
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 59
Branch analysis from position: 56
Branch analysis from position: 57
Branch analysis from position: 51
Branch analysis from position: 49
Branch analysis from position: 47
Branch analysis from position: 41
Branch analysis from position: 35
Branch analysis from position: 29
Branch analysis from position: 60
Branch analysis from position: 60
filename:       /in/L502s
function name:  getTimeSlot
number of ops:  63
compiled vars:  !0 = $str, !1 = $arr, !2 = $final_datetime, !3 = $format, !4 = $d, !5 = $v, !6 = $k, !7 = $prevDatetime, !8 = $nextDateTime, !9 = $prevStr, !10 = $nextStr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        ASSIGN                                                   !1, <array>
   24     2        NOP                                                      
          3        FAST_CONCAT                                      ~12     !0, '+is+not+within+timeslots.'
          4        ASSIGN                                                   !2, ~12
   25     5        ASSIGN                                                   !3, 'Y-m-d+H%3Ai%3As'
   26     6        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
          7        SEND_VAR                                                 !3
          8        SEND_VAR                                                 !0
          9        DO_FCALL                                      0  $15     
         10        ASSIGN                                                   !4, $15
   28    11      > FE_RESET_R                                       $17     !1, ->60
         12    > > FE_FETCH_R                                       ~18     $17, !5, ->60
         13    >   ASSIGN                                                   !6, ~18
   29    14        IS_IDENTICAL                                             !5, !0
         15      > JMPZ                                                     ~20, ->19
   30    16    >   ASSIGN                                                   !2, !5
   31    17      > JMP                                                      ->60
   29    18*       JMP                                                      ->59
   33    19    >   ASSIGN                                                   !7, <false>
   34    20        ASSIGN                                                   !8, <false>
   35    21        ASSIGN                                                   !9, ''
   36    22        ASSIGN                                                   !10, ''
   38    23        SUB                                              ~26     !6, 1
         24        ISSET_ISEMPTY_DIM_OBJ                         0          !1, ~26
         25      > JMPZ                                                     ~27, ->29
   39    26    >   SUB                                              ~28     !6, 1
         27        FETCH_DIM_R                                      ~29     !1, ~28
         28        ASSIGN                                                   !9, ~29
   41    29    >   ADD                                              ~31     !6, 1
         30        ISSET_ISEMPTY_DIM_OBJ                         0          !1, ~31
         31      > JMPZ                                                     ~32, ->35
   42    32    >   ADD                                              ~33     !6, 1
         33        FETCH_DIM_R                                      ~34     !1, ~33
         34        ASSIGN                                                   !10, ~34
   44    35    > > JMPZ                                                     !9, ->41
   45    36    >   INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         37        SEND_VAR                                                 !3
         38        SEND_VAR                                                 !9
         39        DO_FCALL                                      0  $36     
         40        ASSIGN                                                   !7, $36
   47    41    > > JMPZ                                                     !10, ->47
   48    42    >   INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         43        SEND_VAR                                                 !3
         44        SEND_VAR                                                 !10
         45        DO_FCALL                                      0  $38     
         46        ASSIGN                                                   !8, $38
   51    47    > > JMPZ_EX                                          ~40     !7, ->49
         48    >   BOOL                                             ~40     !8
         49    > > JMPZ_EX                                          ~40     ~40, ->51
         50    >   BOOL                                             ~40     !4
         51    > > JMPZ_EX                                          ~40     ~40, ->57
         52    >   IS_SMALLER                                       ~41     !7, !4
         53      > JMPZ_EX                                          ~41     ~41, ->56
         54    >   IS_SMALLER                                       ~42     !4, !8
         55        BOOL                                             ~41     ~42
         56    >   BOOL                                             ~40     ~41
         57    > > JMPZ                                                     ~40, ->59
   52    58    >   ASSIGN                                                   !2, !9
   28    59    > > JMP                                                      ->12
         60    >   FE_FREE                                                  $17
   56    61      > RETURN                                                   !2
   57    62*     > RETURN                                                   null

End of function gettimeslot

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.78 ms | 1009 KiB | 14 Q