3v4l.org

run code in 300+ PHP versions simultaneously
<?php function timeToSeconds($time) { if (!preg_match('~([+-]?)(\d{2}):(\d{2})~', $time, $out)) { echo "failed to parse $time\n"; return 0; } else { return (($out[2] * 3600) + $out[3] * 60) * ($out[1] == '-' ? -1 : 1); } } function secondsToTime($seconds) { $sign = $seconds < 0 ? '-' : ''; $abs = abs($seconds); return $sign . str_pad(floor($abs / 3600), 2, '0', STR_PAD_LEFT) . ':' . str_pad(floor($abs / 60 % 60), 2, '0', STR_PAD_LEFT); } $times = ['-03:34', '+01:00']; $seconds = 0; foreach ($times as $time) { $seconds += timeToSeconds($time); } echo $seconds; echo "\n---\n"; echo secondsToTime($seconds); echo "\n===\n"; $times = ['10:00', '+20:00']; $seconds = 0; foreach ($times as $time) { $seconds += timeToSeconds($time); } echo $seconds; echo "\n---\n"; echo secondsToTime($seconds); echo "\n===\n"; $times = ['+04:43', '03:33', '-10:33']; $seconds = 0; foreach ($times as $time) { $seconds += timeToSeconds($time); } echo $seconds; echo "\n---\n"; echo secondsToTime($seconds);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 20, Position 2 = 26
Branch analysis from position: 20
2 jumps found. (Code = 78) Position 1 = 21, Position 2 = 26
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
Branch analysis from position: 26
2 jumps found. (Code = 77) Position 1 = 37, Position 2 = 43
Branch analysis from position: 37
2 jumps found. (Code = 78) Position 1 = 38, Position 2 = 43
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 43
Branch analysis from position: 26
Branch analysis from position: 9
filename:       /in/e1vAn
function name:  (null)
number of ops:  51
compiled vars:  !0 = $times, !1 = $seconds, !2 = $time
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   ASSIGN                                                   !0, <array>
   17     1        ASSIGN                                                   !1, 0
   18     2      > FE_RESET_R                                       $5      !0, ->9
          3    > > FE_FETCH_R                                               $5, !2, ->9
   19     4    >   INIT_FCALL                                               'timetoseconds'
          5        SEND_VAR                                                 !2
          6        DO_FCALL                                      0  $6      
          7        ASSIGN_OP                                     1          !1, $6
   18     8      > JMP                                                      ->3
          9    >   FE_FREE                                                  $5
   21    10        ECHO                                                     !1
   22    11        ECHO                                                     '%0A---%0A'
   23    12        INIT_FCALL                                               'secondstotime'
         13        SEND_VAR                                                 !1
         14        DO_FCALL                                      0  $8      
         15        ECHO                                                     $8
   24    16        ECHO                                                     '%0A%3D%3D%3D%0A'
   26    17        ASSIGN                                                   !0, <array>
   27    18        ASSIGN                                                   !1, 0
   28    19      > FE_RESET_R                                       $11     !0, ->26
         20    > > FE_FETCH_R                                               $11, !2, ->26
   29    21    >   INIT_FCALL                                               'timetoseconds'
         22        SEND_VAR                                                 !2
         23        DO_FCALL                                      0  $12     
         24        ASSIGN_OP                                     1          !1, $12
   28    25      > JMP                                                      ->20
         26    >   FE_FREE                                                  $11
   31    27        ECHO                                                     !1
   32    28        ECHO                                                     '%0A---%0A'
   33    29        INIT_FCALL                                               'secondstotime'
         30        SEND_VAR                                                 !1
         31        DO_FCALL                                      0  $14     
         32        ECHO                                                     $14
   34    33        ECHO                                                     '%0A%3D%3D%3D%0A'
   36    34        ASSIGN                                                   !0, <array>
   37    35        ASSIGN                                                   !1, 0
   38    36      > FE_RESET_R                                       $17     !0, ->43
         37    > > FE_FETCH_R                                               $17, !2, ->43
   39    38    >   INIT_FCALL                                               'timetoseconds'
         39        SEND_VAR                                                 !2
         40        DO_FCALL                                      0  $18     
         41        ASSIGN_OP                                     1          !1, $18
   38    42      > JMP                                                      ->37
         43    >   FE_FREE                                                  $17
   41    44        ECHO                                                     !1
   42    45        ECHO                                                     '%0A---%0A'
   43    46        INIT_FCALL                                               'secondstotime'
         47        SEND_VAR                                                 !1
         48        DO_FCALL                                      0  $20     
         49        ECHO                                                     $20
         50      > RETURN                                                   1

Function timetoseconds:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/e1vAn
function name:  timeToSeconds
number of ops:  28
compiled vars:  !0 = $time, !1 = $out
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
    3     1        INIT_FCALL                                               'preg_match'
          2        SEND_VAL                                                 '%7E%28%5B%2B-%5D%3F%29%28%5Cd%7B2%7D%29%3A%28%5Cd%7B2%7D%29%7E'
          3        SEND_VAR                                                 !0
          4        SEND_REF                                                 !1
          5        DO_ICALL                                         $2      
          6        BOOL_NOT                                         ~3      $2
          7      > JMPZ                                                     ~3, ->14
    4     8    >   ROPE_INIT                                     3  ~5      'failed+to+parse+'
          9        ROPE_ADD                                      1  ~5      ~5, !0
         10        ROPE_END                                      2  ~4      ~5, '%0A'
         11        ECHO                                                     ~4
    5    12      > RETURN                                                   0
    3    13*       JMP                                                      ->27
    7    14    >   FETCH_DIM_R                                      ~7      !1, 2
         15        MUL                                              ~8      ~7, 3600
         16        FETCH_DIM_R                                      ~9      !1, 3
         17        MUL                                              ~10     ~9, 60
         18        ADD                                              ~11     ~8, ~10
         19        FETCH_DIM_R                                      ~12     !1, 1
         20        IS_EQUAL                                                 ~12, '-'
         21      > JMPZ                                                     ~13, ->24
         22    >   QM_ASSIGN                                        ~14     -1
         23      > JMP                                                      ->25
         24    >   QM_ASSIGN                                        ~14     1
         25    >   MUL                                              ~15     ~11, ~14
         26      > RETURN                                                   ~15
    9    27*     > RETURN                                                   null

End of function timetoseconds

Function secondstotime:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/e1vAn
function name:  secondsToTime
number of ops:  37
compiled vars:  !0 = $seconds, !1 = $sign, !2 = $abs
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
   11     1        IS_SMALLER                                               !0, 0
          2      > JMPZ                                                     ~3, ->5
          3    >   QM_ASSIGN                                        ~4      '-'
          4      > JMP                                                      ->6
          5    >   QM_ASSIGN                                        ~4      ''
          6    >   ASSIGN                                                   !1, ~4
   12     7        INIT_FCALL                                               'abs'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $6      
         10        ASSIGN                                                   !2, $6
   13    11        INIT_FCALL                                               'str_pad'
         12        INIT_FCALL                                               'floor'
         13        DIV                                              ~8      !2, 3600
         14        SEND_VAL                                                 ~8
         15        DO_ICALL                                         $9      
         16        SEND_VAR                                                 $9
         17        SEND_VAL                                                 2
         18        SEND_VAL                                                 '0'
         19        SEND_VAL                                                 0
         20        DO_ICALL                                         $10     
         21        CONCAT                                           ~11     !1, $10
         22        CONCAT                                           ~12     ~11, '%3A'
         23        INIT_FCALL                                               'str_pad'
         24        INIT_FCALL                                               'floor'
         25        DIV                                              ~13     !2, 60
         26        MOD                                              ~14     ~13, 60
         27        SEND_VAL                                                 ~14
         28        DO_ICALL                                         $15     
         29        SEND_VAR                                                 $15
         30        SEND_VAL                                                 2
         31        SEND_VAL                                                 '0'
         32        SEND_VAL                                                 0
         33        DO_ICALL                                         $16     
         34        CONCAT                                           ~17     ~12, $16
         35      > RETURN                                                   ~17
   14    36*     > RETURN                                                   null

End of function secondstotime

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.64 ms | 1016 KiB | 23 Q