3v4l.org

run code in 300+ PHP versions simultaneously
<?php function rangify(array $dates): string { $lastYm = null; $lastN = null; $result = []; foreach ($dates as $date) { $dt = new DateTime($date); $ym = $dt->format('Y-m'); $j = (int) $dt->format('j'); if ($ym === $lastYm) { if (($j - 1) === $lastN) { $ref = preg_replace('/-\d+$/', '', $ref) . "-$j"; } else { $ref .= ",$j"; } } else { unset($ref); $ref = $dt->format('M') . " $j"; $result[] = &$ref; } $lastYm = $ym; $lastN = $j; } return implode(', ', $result); } $tests = [ ['2015-10-27', '2015-10-28', '2015-10-30', '2015-10-31', '2015-11-01'], ['2015-10-31', '2015-11-01', '2015-11-02'], ['2015-10-26', '2015-10-28', '2015-10-29', '2015-10-31', '2015-11-01'], ]; //Oct 30-31, Nov 01 //Oct 31, Nov 1-2 //Oct 29,31, Nov 1 foreach ($tests as $test) { echo rangify($test) . "\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 9
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/KeLsp
function name:  (null)
number of ops:  11
compiled vars:  !0 = $tests, !1 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   ASSIGN                                                   !0, <array>
   38     1      > FE_RESET_R                                       $3      !0, ->9
          2    > > FE_FETCH_R                                               $3, !1, ->9
   39     3    >   INIT_FCALL                                               'rangify'
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $4      
          6        CONCAT                                           ~5      $4, '%0A'
          7        ECHO                                                     ~5
   38     8      > JMP                                                      ->2
          9    >   FE_FREE                                                  $3
   40    10      > RETURN                                                   1

Function rangify:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 51
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 51
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 38
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 34
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
filename:       /in/KeLsp
function name:  rangify
number of ops:  60
compiled vars:  !0 = $dates, !1 = $lastYm, !2 = $lastN, !3 = $result, !4 = $date, !5 = $dt, !6 = $ym, !7 = $j, !8 = $ref
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        ASSIGN                                                   !1, null
    5     2        ASSIGN                                                   !2, null
    6     3        ASSIGN                                                   !3, <array>
    7     4      > FE_RESET_R                                       $12     !0, ->51
          5    > > FE_FETCH_R                                               $12, !4, ->51
    8     6    >   NEW                                              $13     'DateTime'
          7        SEND_VAR_EX                                              !4
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !5, $13
    9    10        INIT_METHOD_CALL                                         !5, 'format'
         11        SEND_VAL_EX                                              'Y-m'
         12        DO_FCALL                                      0  $16     
         13        ASSIGN                                                   !6, $16
   10    14        INIT_METHOD_CALL                                         !5, 'format'
         15        SEND_VAL_EX                                              'j'
         16        DO_FCALL                                      0  $18     
         17        CAST                                          4  ~19     $18
         18        ASSIGN                                                   !7, ~19
   11    19        IS_IDENTICAL                                             !6, !1
         20      > JMPZ                                                     ~21, ->38
   12    21    >   SUB                                              ~22     !7, 1
         22        IS_IDENTICAL                                             !2, ~22
         23      > JMPZ                                                     ~23, ->34
   13    24    >   INIT_FCALL                                               'preg_replace'
         25        SEND_VAL                                                 '%2F-%5Cd%2B%24%2F'
         26        SEND_VAL                                                 ''
         27        SEND_VAR                                                 !8
         28        DO_ICALL                                         $24     
         29        NOP                                                      
         30        FAST_CONCAT                                      ~25     '-', !7
         31        CONCAT                                           ~26     $24, ~25
         32        ASSIGN                                                   !8, ~26
   12    33      > JMP                                                      ->37
   15    34    >   NOP                                                      
         35        FAST_CONCAT                                      ~28     '%2C', !7
         36        ASSIGN_OP                                     8          !8, ~28
   11    37    > > JMP                                                      ->48
   18    38    >   UNSET_CV                                                 !8
   19    39        INIT_METHOD_CALL                                         !5, 'format'
         40        SEND_VAL_EX                                              'M'
         41        DO_FCALL                                      0  $30     
         42        NOP                                                      
         43        FAST_CONCAT                                      ~31     '+', !7
         44        CONCAT                                           ~32     $30, ~31
         45        ASSIGN                                                   !8, ~32
   20    46        FETCH_DIM_W                                      $34     !3
         47        ASSIGN_REF                                               $34, !8
   22    48    >   ASSIGN                                                   !1, !6
   23    49        ASSIGN                                                   !2, !7
    7    50      > JMP                                                      ->5
         51    >   FE_FREE                                                  $12
   25    52        INIT_FCALL                                               'implode'
         53        SEND_VAL                                                 '%2C+'
         54        SEND_VAR                                                 !3
         55        DO_ICALL                                         $38     
         56        VERIFY_RETURN_TYPE                                       $38
         57      > RETURN                                                   $38
   26    58*       VERIFY_RETURN_TYPE                                       
         59*     > RETURN                                                   null

End of function rangify

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
194.29 ms | 1018 KiB | 16 Q