3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); $data = [ '2020-07-14' => [ [ 'start_time' => '14:15:00', 'end_time' => '17:45:00', ],[ 'start_time' => '14:30:00', 'end_time' => '17:30:00', ],[ 'start_time' => '14:30:00', 'end_time' => '17:30:00', ], ], ]; function getSortedDays(array $days): array { return array_map(function (array $day) { array_multisort(array_column($day, 'start_time'), SORT_ASC, $day); return $day; }, $days); } function addTotalAttendedHours(array $days): array { $sortedDays = getSortedDays($days); $days = array_map(function (array $day) { $sum = (new DateTime())->setTimestamp(0); $previousEnd = null; foreach ($day as $time) { $currentStart = new DateTimeImmutable($time['start_time']); $currentEnd = new DateTimeImmutable($time['end_time']); if ($currentEnd < $previousEnd) continue; $sum->add($currentStart->diff($currentEnd)); if ($previousEnd !== null && $currentStart < $previousEnd) { $sum->sub($currentStart->diff($previousEnd)); } $previousEnd = $currentEnd; } $attendedSeconds = $sum->getTimestamp(); $day['total_attended_hours'] = sprintf( '%02u:%02u:%02u', $attendedSeconds / 60 / 60, ($attendedSeconds / 60) % 60, $attendedSeconds % 60 ); return $day; }, $sortedDays); return $days; } var_dump(addTotalAttendedHours($data));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s7PUM
function name:  (null)
number of ops:  8
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   ASSIGN                                                   !0, <array>
   65     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'addtotalattendedhours'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function getsorteddays:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s7PUM
function name:  getSortedDays
number of ops:  10
compiled vars:  !0 = $days
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   22     1        INIT_FCALL                                               'array_map'
          2        DECLARE_LAMBDA_FUNCTION                          ~1      [0]
   26     3        SEND_VAL                                                 ~1
          4        SEND_VAR                                                 !0
   22     5        DO_ICALL                                         $2      
   26     6        VERIFY_RETURN_TYPE                                       $2
          7      > RETURN                                                   $2
   27     8*       VERIFY_RETURN_TYPE                                       
          9*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s7PUM
function name:  {closure}
number of ops:  12
compiled vars:  !0 = $day
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
   23     1        INIT_FCALL                                               'array_multisort'
          2        INIT_FCALL                                               'array_column'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 'start_time'
          5        DO_ICALL                                         $1      
          6        SEND_VAL                                                 $1
          7        SEND_VAL                                                 4
          8        SEND_REF                                                 !0
          9        DO_ICALL                                                 
   25    10      > RETURN                                                   !0
   26    11*     > RETURN                                                   null

End of Dynamic Function 0

End of function getsorteddays

Function addtotalattendedhours:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s7PUM
function name:  addTotalAttendedHours
number of ops:  15
compiled vars:  !0 = $days, !1 = $sortedDays
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
   30     1        INIT_FCALL                                               'getsorteddays'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !1, $2
   32     5        INIT_FCALL                                               'array_map'
          6        DECLARE_LAMBDA_FUNCTION                          ~4      [0]
   60     7        SEND_VAL                                                 ~4
          8        SEND_VAR                                                 !1
   32     9        DO_ICALL                                         $5      
         10        ASSIGN                                                   !0, $5
   62    11        VERIFY_RETURN_TYPE                                       !0
         12      > RETURN                                                   !0
   63    13*       VERIFY_RETURN_TYPE                                       
         14*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 44
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 44
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 25
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 25
2 jumps found. (Code = 46) Position 1 = 33, Position 2 = 35
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 42
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 42
Branch analysis from position: 35
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
filename:       /in/s7PUM
function name:  {closure}
number of ops:  63
compiled vars:  !0 = $day, !1 = $sum, !2 = $previousEnd, !3 = $time, !4 = $currentStart, !5 = $currentEnd, !6 = $attendedSeconds
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
   33     1        NEW                                              $7      'DateTime'
          2        DO_FCALL                                      0          
          3        INIT_METHOD_CALL                                         $7, 'setTimestamp'
          4        SEND_VAL_EX                                              0
          5        DO_FCALL                                      0  $9      
          6        ASSIGN                                                   !1, $9
   34     7        ASSIGN                                                   !2, null
   36     8      > FE_RESET_R                                       $12     !0, ->44
          9    > > FE_FETCH_R                                               $12, !3, ->44
   37    10    >   NEW                                              $13     'DateTimeImmutable'
         11        CHECK_FUNC_ARG                                           
         12        FETCH_DIM_FUNC_ARG                               $14     !3, 'start_time'
         13        SEND_FUNC_ARG                                            $14
         14        DO_FCALL                                      0          
         15        ASSIGN                                                   !4, $13
   38    16        NEW                                              $17     'DateTimeImmutable'
         17        CHECK_FUNC_ARG                                           
         18        FETCH_DIM_FUNC_ARG                               $18     !3, 'end_time'
         19        SEND_FUNC_ARG                                            $18
         20        DO_FCALL                                      0          
         21        ASSIGN                                                   !5, $17
   40    22        IS_SMALLER                                               !5, !2
         23      > JMPZ                                                     ~21, ->25
         24    > > JMP                                                      ->9
   42    25    >   INIT_METHOD_CALL                                         !1, 'add'
         26        INIT_METHOD_CALL                                         !4, 'diff'
         27        SEND_VAR_EX                                              !5
         28        DO_FCALL                                      0  $22     
         29        SEND_VAR_NO_REF_EX                                       $22
         30        DO_FCALL                                      0          
   44    31        TYPE_CHECK                                  1020  ~24     !2
         32      > JMPZ_EX                                          ~24     ~24, ->35
         33    >   IS_SMALLER                                       ~25     !4, !2
         34        BOOL                                             ~24     ~25
         35    > > JMPZ                                                     ~24, ->42
   45    36    >   INIT_METHOD_CALL                                         !1, 'sub'
         37        INIT_METHOD_CALL                                         !4, 'diff'
         38        SEND_VAR_EX                                              !2
         39        DO_FCALL                                      0  $26     
         40        SEND_VAR_NO_REF_EX                                       $26
         41        DO_FCALL                                      0          
   48    42    >   ASSIGN                                                   !2, !5
   36    43      > JMP                                                      ->9
         44    >   FE_FREE                                                  $12
   51    45        INIT_METHOD_CALL                                         !1, 'getTimestamp'
         46        DO_FCALL                                      0  $29     
         47        ASSIGN                                                   !6, $29
   52    48        INIT_FCALL                                               'sprintf'
   53    49        SEND_VAL                                                 '%2502u%3A%2502u%3A%2502u'
   54    50        DIV                                              ~32     !6, 60
         51        DIV                                              ~33     ~32, 60
         52        SEND_VAL                                                 ~33
   55    53        DIV                                              ~34     !6, 60
         54        MOD                                              ~35     ~34, 60
         55        SEND_VAL                                                 ~35
   56    56        MOD                                              ~36     !6, 60
         57        SEND_VAL                                                 ~36
   52    58        DO_ICALL                                         $37     
         59        ASSIGN_DIM                                               !0, 'total_attended_hours'
   56    60        OP_DATA                                                  $37
   59    61      > RETURN                                                   !0
   60    62*     > RETURN                                                   null

End of Dynamic Function 0

End of function addtotalattendedhours

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
244.05 ms | 968 KiB | 20 Q