3v4l.org

run code in 300+ PHP versions simultaneously
<?php $attendance = [ "2019-07-01 08:00:00", "2019-07-01 12:00:00", "2019-07-01 13:00:00", "2019-07-01 17:00:00", ]; /** TEST CASES: // Late $attendance = [ "2019-07-01 08:35:00", "2019-07-01 12:00:00", "2019-07-01 13:00:00", "2019-07-01 17:00:00", ]; // Early $attendance = [ "2019-07-01 07:45:00", "2019-07-01 12:00:00", "2019-07-01 13:00:00", "2019-07-01 17:00:00", ]; */ $schedules = [ [ "time_start" => "08:00:00", "time_end" => "12:00:00", "in_threshold" => 15, "out_threshold" => 15 ], [ "time_start" => "13:00:00", "time_end" => "17:00:00", "in_threshold" => 15, "out_threshold" => 15 ] ]; // If date is between $from and $to function isBetween( $from, $to, $date ) { return ( $date > $from ) && ( $date <= $to ); } // Get ClosestTime in schedules time_start and time_end function getClosestTime( $array, $date ){ foreach( $array as $day ){ $interval[] = abs(strtotime($date) - strtotime($day)); } asort($interval); $closest = key($interval); return $array[$closest]; } // Store time_start and time_end to array foreach($schedules as $schedule){ // Set time_start and time_end with threshold $mytime_start[] = date( "H:i:s", strtotime('+'. $schedule[ 'in_threshold' ] .' minutes', strtotime ( $schedule[ 'time_start' ] ))); $mytime_end[] = date( "H:i:s", strtotime('+'. $schedule[ 'out_threshold' ] .' minutes', strtotime( $schedule[ 'time_end' ] ))); // Get time_start and time_end to array $sched_starts[] = ( $schedule[ 'time_start' ] ); $sched_ends[] = ( $schedule[ 'time_end' ] ); } // Initialized array $total_garner_hours = 0; $total_late = 0; // Get attendance record by 2 for ($index = 0; $index < count($attendance); $index += 2) { $date_start = $attendance[$index]; // Punch In $date_end = $attendance[$index + 1]; // Punch Out // Get Dates from time_in and time_out (for dates cross midnight) $to_date_start = date("Y-m-d", strtotime($date_start)); $to_date_end = date("Y-m-d", strtotime($date_end)); // Get Closest time schedule $time_start = getClosestTime($mytime_start, date('H:i:s',strtotime($date_start))); $time_end = getClosestTime($mytime_end, date('H:i:s',strtotime($date_end))); // Set check in to time start if its greater than time end if ($time_start > $time_end) { $time_start = date("Y-m-d", strtotime($date_start)); } // Set date to datetime from $to_date_start / $to_date_end $time_start = date('Y-m-d H:i:s', strtotime("$to_date_start $time_start")); $time_end = date('Y-m-d H:i:s', strtotime("$to_date_end $time_end")); // Remove threshold Eg. 08:15:00 to 08:00:00 $time_minus_start = date("Y-m-d H:i:s", strtotime("-15 minutes $time_start")); $time_minus_end = date("Y-m-d H:i:s", strtotime("-15 minutes $time_end")); // Sets variables to make it more readable $within_in = $late = strtotime($date_start); $within_out = $early = strtotime($date_end); $exact_in = strtotime($time_minus_start); $exact_out = strtotime($time_minus_end); $late_out = strtotime($time_end); // Check if date_start is Early or Exact if ($date_start > $time_start) { $start = $late; $total_late += 1; }else { $start = $exact_in; } // Check if date_end is Early or Exact ($date_end < $time_end) ? $end = $early : $end = $exact_out; // If date_start is between 08:00:00 and 08:15:00 and date_end is between 12:00:00 and 12:15:00 // return its date if (isBetween($time_minus_start, $time_start, $date_start) && isBetween($time_minus_end, $time_end, $date_end)) { $start = $within_in; $end = $within_out; } else if (isBetween($time_minus_start, $time_start, $date_start) && $date_end > $time_end) { $start = $within_in; $end = $late_out; } // Sum the total garnered hours $total_garner_hours += ($end - $start); } $total_hours = floor($total_garner_hours / 3600); echo ('Total Hours:'.$total_hours); echo '<br>'; echo ('Late: '.$total_late);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 45
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 45
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 213
Branch analysis from position: 213
2 jumps found. (Code = 44) Position 1 = 216, Position 2 = 50
Branch analysis from position: 216
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
2 jumps found. (Code = 43) Position 1 = 97, Position 2 = 105
Branch analysis from position: 97
2 jumps found. (Code = 43) Position 1 = 171, Position 2 = 174
Branch analysis from position: 171
1 jumps found. (Code = 42) Position 1 = 175
Branch analysis from position: 175
2 jumps found. (Code = 43) Position 1 = 177, Position 2 = 180
Branch analysis from position: 177
1 jumps found. (Code = 42) Position 1 = 182
Branch analysis from position: 182
2 jumps found. (Code = 46) Position 1 = 189, Position 2 = 195
Branch analysis from position: 189
2 jumps found. (Code = 43) Position 1 = 196, Position 2 = 199
Branch analysis from position: 196
1 jumps found. (Code = 42) Position 1 = 210
Branch analysis from position: 210
2 jumps found. (Code = 44) Position 1 = 216, Position 2 = 50
Branch analysis from position: 216
Branch analysis from position: 50
Branch analysis from position: 199
2 jumps found. (Code = 46) Position 1 = 205, Position 2 = 207
Branch analysis from position: 205
2 jumps found. (Code = 43) Position 1 = 208, Position 2 = 210
Branch analysis from position: 208
2 jumps found. (Code = 44) Position 1 = 216, Position 2 = 50
Branch analysis from position: 216
Branch analysis from position: 50
Branch analysis from position: 210
Branch analysis from position: 207
Branch analysis from position: 195
Branch analysis from position: 180
2 jumps found. (Code = 46) Position 1 = 189, Position 2 = 195
Branch analysis from position: 189
Branch analysis from position: 195
Branch analysis from position: 174
2 jumps found. (Code = 43) Position 1 = 177, Position 2 = 180
Branch analysis from position: 177
Branch analysis from position: 180
Branch analysis from position: 105
Branch analysis from position: 45
filename:       /in/9MaEc
function name:  (null)
number of ops:  227
compiled vars:  !0 = $attendance, !1 = $schedules, !2 = $schedule, !3 = $mytime_start, !4 = $mytime_end, !5 = $sched_starts, !6 = $sched_ends, !7 = $total_garner_hours, !8 = $total_late, !9 = $index, !10 = $date_start, !11 = $date_end, !12 = $to_date_start, !13 = $to_date_end, !14 = $time_start, !15 = $time_end, !16 = $time_minus_start, !17 = $time_minus_end, !18 = $within_in, !19 = $late, !20 = $within_out, !21 = $early, !22 = $exact_in, !23 = $exact_out, !24 = $late_out, !25 = $start, !26 = $end, !27 = $total_hours
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   28     1        ASSIGN                                                   !1, <array>
   62     2      > FE_RESET_R                                       $30     !1, ->45
          3    > > FE_FETCH_R                                               $30, !2, ->45
   65     4    >   INIT_FCALL                                               'date'
          5        SEND_VAL                                                 'H%3Ai%3As'
   66     6        INIT_FCALL                                               'strtotime'
          7        FETCH_DIM_R                                      ~32     !2, 'in_threshold'
          8        CONCAT                                           ~33     '%2B', ~32
          9        CONCAT                                           ~34     ~33, '+minutes'
         10        SEND_VAL                                                 ~34
   67    11        INIT_FCALL                                               'strtotime'
         12        FETCH_DIM_R                                      ~35     !2, 'time_start'
         13        SEND_VAL                                                 ~35
         14        DO_ICALL                                         $36     
         15        SEND_VAR                                                 $36
         16        DO_ICALL                                         $37     
         17        SEND_VAR                                                 $37
         18        DO_ICALL                                         $38     
   65    19        ASSIGN_DIM                                               !3
   67    20        OP_DATA                                                  $38
   69    21        INIT_FCALL                                               'date'
         22        SEND_VAL                                                 'H%3Ai%3As'
   70    23        INIT_FCALL                                               'strtotime'
         24        FETCH_DIM_R                                      ~40     !2, 'out_threshold'
         25        CONCAT                                           ~41     '%2B', ~40
         26        CONCAT                                           ~42     ~41, '+minutes'
         27        SEND_VAL                                                 ~42
   71    28        INIT_FCALL                                               'strtotime'
         29        FETCH_DIM_R                                      ~43     !2, 'time_end'
         30        SEND_VAL                                                 ~43
         31        DO_ICALL                                         $44     
         32        SEND_VAR                                                 $44
         33        DO_ICALL                                         $45     
         34        SEND_VAR                                                 $45
         35        DO_ICALL                                         $46     
   69    36        ASSIGN_DIM                                               !4
   71    37        OP_DATA                                                  $46
   74    38        FETCH_DIM_R                                      ~48     !2, 'time_start'
         39        ASSIGN_DIM                                               !5
         40        OP_DATA                                                  ~48
   75    41        FETCH_DIM_R                                      ~50     !2, 'time_end'
         42        ASSIGN_DIM                                               !6
         43        OP_DATA                                                  ~50
   62    44      > JMP                                                      ->3
         45    >   FE_FREE                                                  $30
   79    46        ASSIGN                                                   !7, 0
   80    47        ASSIGN                                                   !8, 0
   83    48        ASSIGN                                                   !9, 0
         49      > JMP                                                      ->213
   85    50    >   FETCH_DIM_R                                      ~54     !0, !9
         51        ASSIGN                                                   !10, ~54
   86    52        ADD                                              ~56     !9, 1
         53        FETCH_DIM_R                                      ~57     !0, ~56
         54        ASSIGN                                                   !11, ~57
   89    55        INIT_FCALL                                               'date'
         56        SEND_VAL                                                 'Y-m-d'
         57        INIT_FCALL                                               'strtotime'
         58        SEND_VAR                                                 !10
         59        DO_ICALL                                         $59     
         60        SEND_VAR                                                 $59
         61        DO_ICALL                                         $60     
         62        ASSIGN                                                   !12, $60
   90    63        INIT_FCALL                                               'date'
         64        SEND_VAL                                                 'Y-m-d'
         65        INIT_FCALL                                               'strtotime'
         66        SEND_VAR                                                 !11
         67        DO_ICALL                                         $62     
         68        SEND_VAR                                                 $62
         69        DO_ICALL                                         $63     
         70        ASSIGN                                                   !13, $63
   93    71        INIT_FCALL                                               'getclosesttime'
         72        SEND_VAR                                                 !3
         73        INIT_FCALL                                               'date'
         74        SEND_VAL                                                 'H%3Ai%3As'
         75        INIT_FCALL                                               'strtotime'
         76        SEND_VAR                                                 !10
         77        DO_ICALL                                         $65     
         78        SEND_VAR                                                 $65
         79        DO_ICALL                                         $66     
         80        SEND_VAR                                                 $66
         81        DO_FCALL                                      0  $67     
         82        ASSIGN                                                   !14, $67
   94    83        INIT_FCALL                                               'getclosesttime'
         84        SEND_VAR                                                 !4
         85        INIT_FCALL                                               'date'
         86        SEND_VAL                                                 'H%3Ai%3As'
         87        INIT_FCALL                                               'strtotime'
         88        SEND_VAR                                                 !11
         89        DO_ICALL                                         $69     
         90        SEND_VAR                                                 $69
         91        DO_ICALL                                         $70     
         92        SEND_VAR                                                 $70
         93        DO_FCALL                                      0  $71     
         94        ASSIGN                                                   !15, $71
   97    95        IS_SMALLER                                               !15, !14
         96      > JMPZ                                                     ~73, ->105
   98    97    >   INIT_FCALL                                               'date'
         98        SEND_VAL                                                 'Y-m-d'
         99        INIT_FCALL                                               'strtotime'
        100        SEND_VAR                                                 !10
        101        DO_ICALL                                         $74     
        102        SEND_VAR                                                 $74
        103        DO_ICALL                                         $75     
        104        ASSIGN                                                   !14, $75
  102   105    >   INIT_FCALL                                               'date'
        106        SEND_VAL                                                 'Y-m-d+H%3Ai%3As'
        107        INIT_FCALL                                               'strtotime'
        108        ROPE_INIT                                     3  ~78     !12
        109        ROPE_ADD                                      1  ~78     ~78, '+'
        110        ROPE_END                                      2  ~77     ~78, !14
        111        SEND_VAL                                                 ~77
        112        DO_ICALL                                         $80     
        113        SEND_VAR                                                 $80
        114        DO_ICALL                                         $81     
        115        ASSIGN                                                   !14, $81
  103   116        INIT_FCALL                                               'date'
        117        SEND_VAL                                                 'Y-m-d+H%3Ai%3As'
        118        INIT_FCALL                                               'strtotime'
        119        ROPE_INIT                                     3  ~84     !13
        120        ROPE_ADD                                      1  ~84     ~84, '+'
        121        ROPE_END                                      2  ~83     ~84, !15
        122        SEND_VAL                                                 ~83
        123        DO_ICALL                                         $86     
        124        SEND_VAR                                                 $86
        125        DO_ICALL                                         $87     
        126        ASSIGN                                                   !15, $87
  106   127        INIT_FCALL                                               'date'
        128        SEND_VAL                                                 'Y-m-d+H%3Ai%3As'
        129        INIT_FCALL                                               'strtotime'
        130        NOP                                                      
        131        FAST_CONCAT                                      ~89     '-15+minutes+', !14
        132        SEND_VAL                                                 ~89
        133        DO_ICALL                                         $90     
        134        SEND_VAR                                                 $90
        135        DO_ICALL                                         $91     
        136        ASSIGN                                                   !16, $91
  107   137        INIT_FCALL                                               'date'
        138        SEND_VAL                                                 'Y-m-d+H%3Ai%3As'
        139        INIT_FCALL                                               'strtotime'
        140        NOP                                                      
        141        FAST_CONCAT                                      ~93     '-15+minutes+', !15
        142        SEND_VAL                                                 ~93
        143        DO_ICALL                                         $94     
        144        SEND_VAR                                                 $94
        145        DO_ICALL                                         $95     
        146        ASSIGN                                                   !17, $95
  110   147        INIT_FCALL                                               'strtotime'
        148        SEND_VAR                                                 !10
        149        DO_ICALL                                         $97     
        150        ASSIGN                                           ~98     !19, $97
        151        ASSIGN                                                   !18, ~98
  111   152        INIT_FCALL                                               'strtotime'
        153        SEND_VAR                                                 !11
        154        DO_ICALL                                         $100    
        155        ASSIGN                                           ~101    !21, $100
        156        ASSIGN                                                   !20, ~101
  112   157        INIT_FCALL                                               'strtotime'
        158        SEND_VAR                                                 !16
        159        DO_ICALL                                         $103    
        160        ASSIGN                                                   !22, $103
  113   161        INIT_FCALL                                               'strtotime'
        162        SEND_VAR                                                 !17
        163        DO_ICALL                                         $105    
        164        ASSIGN                                                   !23, $105
  114   165        INIT_FCALL                                               'strtotime'
        166        SEND_VAR                                                 !15
        167        DO_ICALL                                         $107    
        168        ASSIGN                                                   !24, $107
  117   169        IS_SMALLER                                               !14, !10
        170      > JMPZ                                                     ~109, ->174
  118   171    >   ASSIGN                                                   !25, !19
  119   172        ASSIGN_OP                                     1          !8, 1
        173      > JMP                                                      ->175
  122   174    >   ASSIGN                                                   !25, !22
  126   175    >   IS_SMALLER                                               !11, !15
        176      > JMPZ                                                     ~113, ->180
        177    >   ASSIGN                                           ~114    !26, !21
        178        QM_ASSIGN                                        ~115    ~114
        179      > JMP                                                      ->182
        180    >   ASSIGN                                           ~116    !26, !23
        181        QM_ASSIGN                                        ~115    ~116
        182    >   FREE                                                     ~115
  130   183        INIT_FCALL                                               'isbetween'
        184        SEND_VAR                                                 !16
        185        SEND_VAR                                                 !14
        186        SEND_VAR                                                 !10
        187        DO_FCALL                                      0  $117    
        188      > JMPZ_EX                                          ~118    $117, ->195
  131   189    >   INIT_FCALL                                               'isbetween'
        190        SEND_VAR                                                 !17
        191        SEND_VAR                                                 !15
        192        SEND_VAR                                                 !11
        193        DO_FCALL                                      0  $119    
        194        BOOL                                             ~118    $119
        195    > > JMPZ                                                     ~118, ->199
  132   196    >   ASSIGN                                                   !25, !18
  133   197        ASSIGN                                                   !26, !20
        198      > JMP                                                      ->210
  135   199    >   INIT_FCALL                                               'isbetween'
        200        SEND_VAR                                                 !16
        201        SEND_VAR                                                 !14
        202        SEND_VAR                                                 !10
        203        DO_FCALL                                      0  $122    
        204      > JMPZ_EX                                          ~123    $122, ->207
        205    >   IS_SMALLER                                       ~124    !15, !11
        206        BOOL                                             ~123    ~124
        207    > > JMPZ                                                     ~123, ->210
  136   208    >   ASSIGN                                                   !25, !18
  137   209        ASSIGN                                                   !26, !24
  141   210    >   SUB                                              ~127    !26, !25
        211        ASSIGN_OP                                     1          !7, ~127
   83   212        ASSIGN_OP                                     1          !9, 2
        213    >   COUNT                                            ~130    !0
        214        IS_SMALLER                                               !9, ~130
        215      > JMPNZ                                                    ~131, ->50
  145   216    >   INIT_FCALL                                               'floor'
        217        DIV                                              ~132    !7, 3600
        218        SEND_VAL                                                 ~132
        219        DO_ICALL                                         $133    
        220        ASSIGN                                                   !27, $133
  147   221        CONCAT                                           ~135    'Total+Hours%3A', !27
        222        ECHO                                                     ~135
  148   223        ECHO                                                     '%3Cbr%3E'
  149   224        CONCAT                                           ~136    'Late%3A+', !8
        225        ECHO                                                     ~136
        226      > RETURN                                                   1

Function isbetween:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/9MaEc
function name:  isBetween
number of ops:  9
compiled vars:  !0 = $from, !1 = $to, !2 = $date
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   45     3        IS_SMALLER                                       ~3      !0, !2
          4      > JMPZ_EX                                          ~3      ~3, ->7
          5    >   IS_SMALLER_OR_EQUAL                              ~4      !2, !1
          6        BOOL                                             ~3      ~4
          7    > > RETURN                                                   ~3
   46     8*     > RETURN                                                   null

End of function isbetween

Function getclosesttime:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 17
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 17
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/9MaEc
function name:  getClosestTime
number of ops:  28
compiled vars:  !0 = $array, !1 = $date, !2 = $day, !3 = $interval, !4 = $closest
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   51     2      > FE_RESET_R                                       $5      !0, ->17
          3    > > FE_FETCH_R                                               $5, !2, ->17
   52     4    >   INIT_FCALL                                               'abs'
          5        INIT_FCALL                                               'strtotime'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $7      
          8        INIT_FCALL                                               'strtotime'
          9        SEND_VAR                                                 !2
         10        DO_ICALL                                         $8      
         11        SUB                                              ~9      $7, $8
         12        SEND_VAL                                                 ~9
         13        DO_ICALL                                         $10     
         14        ASSIGN_DIM                                               !3
         15        OP_DATA                                                  $10
   51    16      > JMP                                                      ->3
         17    >   FE_FREE                                                  $5
   55    18        INIT_FCALL                                               'asort'
         19        SEND_REF                                                 !3
         20        DO_ICALL                                                 
   56    21        INIT_FCALL                                               'key'
         22        SEND_VAR                                                 !3
         23        DO_ICALL                                         $12     
         24        ASSIGN                                                   !4, $12
   58    25        FETCH_DIM_R                                      ~14     !0, !4
         26      > RETURN                                                   ~14
   59    27*     > RETURN                                                   null

End of function getclosesttime

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
182.84 ms | 1423 KiB | 30 Q