3v4l.org

run code in 300+ PHP versions simultaneously
<?php function formatDates($dates) { $output = []; $evening_ranges = []; $matinees = []; $last_evening_date = null; foreach ($dates as $date) { $dateTime = new DateTime($date); $time = $dateTime->format('ga'); if ($time == '2pm') { $matinees[] = $dateTime; } else { if ($last_evening_date && $last_evening_date->modify('+1 day')->format('Y-m-d') != $dateTime->format('Y-m-d')) { if (!empty($evening_ranges)) { $output[] = formatRange($evening_ranges); } $evening_ranges = []; } $evening_ranges[] = $dateTime; $last_evening_date = clone $dateTime; } } if (!empty($evening_ranges)) { $output[] = formatRange($evening_ranges); } foreach ($matinees as $matinee) { $output[] = [$matinee]; } usort($output, function($a, $b) { return $a[0] <=> $b[0]; }); return implode(', ', array_map('formatOutput', $output)); } function formatRange($range) { $start = $range[0]; $end = end($range); if ($start->format('M') == $end->format('M')) { return [$start, $end, $start->format('ga')]; } else { return [$start, $end, $start->format('ga')]; } } function formatOutput($range) { if (count($range) == 1) { return $range[0]->format('jS M ga'); } else { $start = $range[0]; $end = $range[1]; $time = $range[2]; if ($start->format('M') == $end->format('M')) { return $start->format('jS') . '-' . $end->format('jS M') . ' ' . $time; } else { return $start->format('jS M') . '-' . $end->format('jS M') . ' ' . $time; } } } $dates = [ '2014-01-20 20:00:00', '2014-01-21 20:00:00', '2014-01-22 14:00:00', '2014-01-22 20:00:00', '2014-01-23 20:00:00', '2014-01-25 20:00:00', '2014-01-26 20:00:00', '2014-01-31 20:00:00', '2014-02-01 20:00:00' ]; echo formatDates($dates); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pXrGU
function name:  (null)
number of ops:  6
compiled vars:  !0 = $dates
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   69     0  E >   ASSIGN                                                   !0, <array>
   81     1        INIT_FCALL                                               'formatdates'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $2      
          4        ECHO                                                     $2
   83     5      > RETURN                                                   1

Function formatdates:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 47
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 47
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 20
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 20
2 jumps found. (Code = 46) Position 1 = 21, Position 2 = 32
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 42
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 41
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 41
Branch analysis from position: 42
Branch analysis from position: 32
Branch analysis from position: 47
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 56
Branch analysis from position: 51
2 jumps found. (Code = 77) Position 1 = 57, Position 2 = 62
Branch analysis from position: 57
2 jumps found. (Code = 78) Position 1 = 58, Position 2 = 62
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
Branch analysis from position: 62
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 62
Branch analysis from position: 56
Branch analysis from position: 47
filename:       /in/pXrGU
function name:  formatDates
number of ops:  78
compiled vars:  !0 = $dates, !1 = $output, !2 = $evening_ranges, !3 = $matinees, !4 = $last_evening_date, !5 = $date, !6 = $dateTime, !7 = $time, !8 = $matinee
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        ASSIGN                                                   !1, <array>
    5     2        ASSIGN                                                   !2, <array>
    6     3        ASSIGN                                                   !3, <array>
    7     4        ASSIGN                                                   !4, null
    9     5      > FE_RESET_R                                       $13     !0, ->47
          6    > > FE_FETCH_R                                               $13, !5, ->47
   10     7    >   NEW                                              $14     'DateTime'
          8        SEND_VAR_EX                                              !5
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !6, $14
   11    11        INIT_METHOD_CALL                                         !6, 'format'
         12        SEND_VAL_EX                                              'ga'
         13        DO_FCALL                                      0  $17     
         14        ASSIGN                                                   !7, $17
   13    15        IS_EQUAL                                                 !7, '2pm'
         16      > JMPZ                                                     ~19, ->20
   14    17    >   ASSIGN_DIM                                               !3
         18        OP_DATA                                                  !6
   13    19      > JMP                                                      ->46
   16    20    > > JMPZ_EX                                          ~21     !4, ->32
         21    >   INIT_METHOD_CALL                                         !4, 'modify'
         22        SEND_VAL_EX                                              '%2B1+day'
         23        DO_FCALL                                      0  $22     
         24        INIT_METHOD_CALL                                         $22, 'format'
         25        SEND_VAL_EX                                              'Y-m-d'
         26        DO_FCALL                                      0  $23     
         27        INIT_METHOD_CALL                                         !6, 'format'
         28        SEND_VAL_EX                                              'Y-m-d'
         29        DO_FCALL                                      0  $24     
         30        IS_NOT_EQUAL                                     ~25     $23, $24
         31        BOOL                                             ~21     ~25
         32    > > JMPZ                                                     ~21, ->42
   17    33    >   ISSET_ISEMPTY_CV                                 ~26     !2
         34        BOOL_NOT                                         ~27     ~26
         35      > JMPZ                                                     ~27, ->41
   18    36    >   INIT_FCALL_BY_NAME                                       'formatRange'
         37        SEND_VAR_EX                                              !2
         38        DO_FCALL                                      0  $29     
         39        ASSIGN_DIM                                               !1
         40        OP_DATA                                                  $29
   20    41    >   ASSIGN                                                   !2, <array>
   22    42    >   ASSIGN_DIM                                               !2
         43        OP_DATA                                                  !6
   23    44        CLONE                                            ~32     !6
         45        ASSIGN                                                   !4, ~32
    9    46    > > JMP                                                      ->6
         47    >   FE_FREE                                                  $13
   27    48        ISSET_ISEMPTY_CV                                 ~34     !2
         49        BOOL_NOT                                         ~35     ~34
         50      > JMPZ                                                     ~35, ->56
   28    51    >   INIT_FCALL_BY_NAME                                       'formatRange'
         52        SEND_VAR_EX                                              !2
         53        DO_FCALL                                      0  $37     
         54        ASSIGN_DIM                                               !1
         55        OP_DATA                                                  $37
   31    56    > > FE_RESET_R                                       $38     !3, ->62
         57    > > FE_FETCH_R                                               $38, !8, ->62
   32    58    >   INIT_ARRAY                                       ~40     !8
         59        ASSIGN_DIM                                               !1
         60        OP_DATA                                                  ~40
   31    61      > JMP                                                      ->57
         62    >   FE_FREE                                                  $38
   35    63        INIT_FCALL                                               'usort'
         64        SEND_REF                                                 !1
         65        DECLARE_LAMBDA_FUNCTION                          ~41     [0]
   37    66        SEND_VAL                                                 ~41
   35    67        DO_ICALL                                                 
   39    68        INIT_FCALL                                               'implode'
         69        SEND_VAL                                                 '%2C+'
         70        INIT_FCALL                                               'array_map'
         71        SEND_VAL                                                 'formatOutput'
         72        SEND_VAR                                                 !1
         73        DO_ICALL                                         $43     
         74        SEND_VAR                                                 $43
         75        DO_ICALL                                         $44     
         76      > RETURN                                                   $44
   40    77*     > 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/pXrGU
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   36     2        FETCH_DIM_R                                      ~2      !0, 0
          3        FETCH_DIM_R                                      ~3      !1, 0
          4        SPACESHIP                                        ~4      ~2, ~3
          5      > RETURN                                                   ~4
   37     6*     > RETURN                                                   null

End of Dynamic Function 0

End of function formatdates

Function formatrange:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 23
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pXrGU
function name:  formatRange
number of ops:  31
compiled vars:  !0 = $range, !1 = $start, !2 = $end
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
   43     1        FETCH_DIM_R                                      ~3      !0, 0
          2        ASSIGN                                                   !1, ~3
   44     3        INIT_FCALL                                               'end'
          4        SEND_REF                                                 !0
          5        DO_ICALL                                         $5      
          6        ASSIGN                                                   !2, $5
   46     7        INIT_METHOD_CALL                                         !1, 'format'
          8        SEND_VAL_EX                                              'M'
          9        DO_FCALL                                      0  $7      
         10        INIT_METHOD_CALL                                         !2, 'format'
         11        SEND_VAL_EX                                              'M'
         12        DO_FCALL                                      0  $8      
         13        IS_EQUAL                                                 $7, $8
         14      > JMPZ                                                     ~9, ->23
   47    15    >   INIT_ARRAY                                       ~10     !1
         16        ADD_ARRAY_ELEMENT                                ~10     !2
         17        INIT_METHOD_CALL                                         !1, 'format'
         18        SEND_VAL_EX                                              'ga'
         19        DO_FCALL                                      0  $11     
         20        ADD_ARRAY_ELEMENT                                ~10     $11
         21      > RETURN                                                   ~10
   46    22*       JMP                                                      ->30
   49    23    >   INIT_ARRAY                                       ~12     !1
         24        ADD_ARRAY_ELEMENT                                ~12     !2
         25        INIT_METHOD_CALL                                         !1, 'format'
         26        SEND_VAL_EX                                              'ga'
         27        DO_FCALL                                      0  $13     
         28        ADD_ARRAY_ELEMENT                                ~12     $13
         29      > RETURN                                                   ~12
   51    30*     > RETURN                                                   null

End of function formatrange

Function formatoutput:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 36
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pXrGU
function name:  formatOutput
number of ops:  48
compiled vars:  !0 = $range, !1 = $start, !2 = $end, !3 = $time
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   RECV                                             !0      
   54     1        COUNT                                            ~4      !0
          2        IS_EQUAL                                                 ~4, 1
          3      > JMPZ                                                     ~5, ->10
   55     4    >   FETCH_DIM_R                                      ~6      !0, 0
          5        INIT_METHOD_CALL                                         ~6, 'format'
          6        SEND_VAL_EX                                              'jS+M+ga'
          7        DO_FCALL                                      0  $7      
          8      > RETURN                                                   $7
   54     9*       JMP                                                      ->47
   57    10    >   FETCH_DIM_R                                      ~8      !0, 0
         11        ASSIGN                                                   !1, ~8
   58    12        FETCH_DIM_R                                      ~10     !0, 1
         13        ASSIGN                                                   !2, ~10
   59    14        FETCH_DIM_R                                      ~12     !0, 2
         15        ASSIGN                                                   !3, ~12
   61    16        INIT_METHOD_CALL                                         !1, 'format'
         17        SEND_VAL_EX                                              'M'
         18        DO_FCALL                                      0  $14     
         19        INIT_METHOD_CALL                                         !2, 'format'
         20        SEND_VAL_EX                                              'M'
         21        DO_FCALL                                      0  $15     
         22        IS_EQUAL                                                 $14, $15
         23      > JMPZ                                                     ~16, ->36
   62    24    >   INIT_METHOD_CALL                                         !1, 'format'
         25        SEND_VAL_EX                                              'jS'
         26        DO_FCALL                                      0  $17     
         27        CONCAT                                           ~18     $17, '-'
         28        INIT_METHOD_CALL                                         !2, 'format'
         29        SEND_VAL_EX                                              'jS+M'
         30        DO_FCALL                                      0  $19     
         31        CONCAT                                           ~20     ~18, $19
         32        CONCAT                                           ~21     ~20, '+'
         33        CONCAT                                           ~22     ~21, !3
         34      > RETURN                                                   ~22
   61    35*       JMP                                                      ->47
   64    36    >   INIT_METHOD_CALL                                         !1, 'format'
         37        SEND_VAL_EX                                              'jS+M'
         38        DO_FCALL                                      0  $23     
         39        CONCAT                                           ~24     $23, '-'
         40        INIT_METHOD_CALL                                         !2, 'format'
         41        SEND_VAL_EX                                              'jS+M'
         42        DO_FCALL                                      0  $25     
         43        CONCAT                                           ~26     ~24, $25
         44        CONCAT                                           ~27     ~26, '+'
         45        CONCAT                                           ~28     ~27, !3
         46      > RETURN                                                   ~28
   67    47*     > RETURN                                                   null

End of function formatoutput

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.7 ms | 1022 KiB | 18 Q