3v4l.org

run code in 300+ PHP versions simultaneously
<?php $toParse = [ [new DateTime('2021-10-3')], [new DateTime('2022-01-25'),new DateTime('2022-01-26'),new DateTime('2022-01-27')], [new DateTime('2021-09-19'),new DateTime('2021-09-22')], [new DateTime('2022-02-28'),new DateTime('2022-03-01'),new DateTime('2022-03-02')], [new DateTime('2022-04-02'),new DateTime('2022-04-03'),new DateTime('2022-04-04'),new DateTime('2022-06-10'),new DateTime('2022-06-11'),new DateTime('2022-06-12'),new DateTime('2022-06-13')], [new DateTime('2021-12-01'),new DateTime('2021-12-02'),new DateTime('2021-12-03'),new DateTime('2021-12-08')] ]; function produceDateString(array $dates): string { // sort the dates sort($dates); // create an array of arrays that contain ranges of consecutive days $ranges = []; $currentRange = []; foreach ($dates as $date) { if(empty($currentRange) || consecutive(end($currentRange), $date)) { $currentRange[] = $date; } else { $ranges[] = $currentRange; $currentRange = [$date]; } } $ranges[] = $currentRange; // create the output string $output = ''; $previous = null; foreach ($ranges as $range) { // add a comma between each range if (!empty($output)) { $output .= ', '; } // the long format should be used on the first occurrence of the loop // or when the month of first date in the range doesn't match // the month of the last date in the previous range $format = $previous === null || end($previous)->format('m') !== reset($range)->format('m') ? 'M. j' : 'j'; // the output differes when there are 1 or multiple dates in a range if (count($range) > 1) { // the output differs when the end and start are in the sane month $output .= sameMonth(reset($range), end($range)) ? reset($range)->format($format).'-'.end($range)->format('j') : reset($range)->format('M. j').'-'.end($range)->format('M. j'); } else { $output .= reset($range)->format($format); } $previous = $range; } return $output; } function consecutive(DateTime $t1, DateTime $t2): bool { $t1->setTime(0, 0, 0, 0); $t2->setTime(0, 0, 0, 0); return(abs($t2->getTimestamp() - $t1->getTimestamp()) < 87000); } function sameMonth(DateTime $t1, DateTime $t2): bool { return $t1->format('Y-m') === $t2->format('Y-m'); } foreach ($toParse as $dates) { echo produceDateString($dates)."\r\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 88, Position 2 = 95
Branch analysis from position: 88
2 jumps found. (Code = 78) Position 1 = 89, Position 2 = 95
Branch analysis from position: 89
1 jumps found. (Code = 42) Position 1 = 88
Branch analysis from position: 88
Branch analysis from position: 95
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 95
filename:       /in/8CDjX
function name:  (null)
number of ops:  97
compiled vars:  !0 = $toParse, !1 = $dates
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   NEW                                              $2      'DateTime'
          1        SEND_VAL_EX                                              '2021-10-3'
          2        DO_FCALL                                      0          
          3        INIT_ARRAY                                       ~4      $2
          4        INIT_ARRAY                                       ~5      ~4
    5     5        NEW                                              $6      'DateTime'
          6        SEND_VAL_EX                                              '2022-01-25'
          7        DO_FCALL                                      0          
          8        INIT_ARRAY                                       ~8      $6
          9        NEW                                              $9      'DateTime'
         10        SEND_VAL_EX                                              '2022-01-26'
         11        DO_FCALL                                      0          
         12        ADD_ARRAY_ELEMENT                                ~8      $9
         13        NEW                                              $11     'DateTime'
         14        SEND_VAL_EX                                              '2022-01-27'
         15        DO_FCALL                                      0          
         16        ADD_ARRAY_ELEMENT                                ~8      $11
         17        ADD_ARRAY_ELEMENT                                ~5      ~8
    6    18        NEW                                              $13     'DateTime'
         19        SEND_VAL_EX                                              '2021-09-19'
         20        DO_FCALL                                      0          
         21        INIT_ARRAY                                       ~15     $13
         22        NEW                                              $16     'DateTime'
         23        SEND_VAL_EX                                              '2021-09-22'
         24        DO_FCALL                                      0          
         25        ADD_ARRAY_ELEMENT                                ~15     $16
         26        ADD_ARRAY_ELEMENT                                ~5      ~15
    7    27        NEW                                              $18     'DateTime'
         28        SEND_VAL_EX                                              '2022-02-28'
         29        DO_FCALL                                      0          
         30        INIT_ARRAY                                       ~20     $18
         31        NEW                                              $21     'DateTime'
         32        SEND_VAL_EX                                              '2022-03-01'
         33        DO_FCALL                                      0          
         34        ADD_ARRAY_ELEMENT                                ~20     $21
         35        NEW                                              $23     'DateTime'
         36        SEND_VAL_EX                                              '2022-03-02'
         37        DO_FCALL                                      0          
         38        ADD_ARRAY_ELEMENT                                ~20     $23
         39        ADD_ARRAY_ELEMENT                                ~5      ~20
    8    40        NEW                                              $25     'DateTime'
         41        SEND_VAL_EX                                              '2022-04-02'
         42        DO_FCALL                                      0          
         43        INIT_ARRAY                                       ~27     $25
         44        NEW                                              $28     'DateTime'
         45        SEND_VAL_EX                                              '2022-04-03'
         46        DO_FCALL                                      0          
         47        ADD_ARRAY_ELEMENT                                ~27     $28
         48        NEW                                              $30     'DateTime'
         49        SEND_VAL_EX                                              '2022-04-04'
         50        DO_FCALL                                      0          
         51        ADD_ARRAY_ELEMENT                                ~27     $30
         52        NEW                                              $32     'DateTime'
         53        SEND_VAL_EX                                              '2022-06-10'
         54        DO_FCALL                                      0          
         55        ADD_ARRAY_ELEMENT                                ~27     $32
         56        NEW                                              $34     'DateTime'
         57        SEND_VAL_EX                                              '2022-06-11'
         58        DO_FCALL                                      0          
         59        ADD_ARRAY_ELEMENT                                ~27     $34
         60        NEW                                              $36     'DateTime'
         61        SEND_VAL_EX                                              '2022-06-12'
         62        DO_FCALL                                      0          
         63        ADD_ARRAY_ELEMENT                                ~27     $36
         64        NEW                                              $38     'DateTime'
         65        SEND_VAL_EX                                              '2022-06-13'
         66        DO_FCALL                                      0          
         67        ADD_ARRAY_ELEMENT                                ~27     $38
         68        ADD_ARRAY_ELEMENT                                ~5      ~27
    9    69        NEW                                              $40     'DateTime'
         70        SEND_VAL_EX                                              '2021-12-01'
         71        DO_FCALL                                      0          
         72        INIT_ARRAY                                       ~42     $40
         73        NEW                                              $43     'DateTime'
         74        SEND_VAL_EX                                              '2021-12-02'
         75        DO_FCALL                                      0          
         76        ADD_ARRAY_ELEMENT                                ~42     $43
         77        NEW                                              $45     'DateTime'
         78        SEND_VAL_EX                                              '2021-12-03'
         79        DO_FCALL                                      0          
         80        ADD_ARRAY_ELEMENT                                ~42     $45
         81        NEW                                              $47     'DateTime'
         82        SEND_VAL_EX                                              '2021-12-08'
         83        DO_FCALL                                      0          
         84        ADD_ARRAY_ELEMENT                                ~42     $47
         85        ADD_ARRAY_ELEMENT                                ~5      ~42
    3    86        ASSIGN                                                   !0, ~5
   74    87      > FE_RESET_R                                       $50     !0, ->95
         88    > > FE_FETCH_R                                               $50, !1, ->95
   75    89    >   INIT_FCALL                                               'producedatestring'
         90        SEND_VAR                                                 !1
         91        DO_FCALL                                      0  $51     
         92        CONCAT                                           ~52     $51, '%0D%0A'
         93        ECHO                                                     ~52
   74    94      > JMP                                                      ->88
         95    >   FE_FREE                                                  $50
   76    96      > RETURN                                                   1

Function producedatestring:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 27
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 27
Branch analysis from position: 8
2 jumps found. (Code = 47) Position 1 = 10, Position 2 = 18
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 22
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 18
Branch analysis from position: 27
2 jumps found. (Code = 77) Position 1 = 33, Position 2 = 115
Branch analysis from position: 33
2 jumps found. (Code = 78) Position 1 = 34, Position 2 = 115
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 38
Branch analysis from position: 37
2 jumps found. (Code = 47) Position 1 = 40, Position 2 = 54
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 55, Position 2 = 57
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 106
Branch analysis from position: 62
2 jumps found. (Code = 43) Position 1 = 73, Position 2 = 89
Branch analysis from position: 73
1 jumps found. (Code = 42) Position 1 = 104
Branch analysis from position: 104
1 jumps found. (Code = 42) Position 1 = 113
Branch analysis from position: 113
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
Branch analysis from position: 89
1 jumps found. (Code = 42) Position 1 = 113
Branch analysis from position: 113
Branch analysis from position: 106
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
Branch analysis from position: 57
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 106
Branch analysis from position: 62
Branch analysis from position: 106
Branch analysis from position: 54
Branch analysis from position: 38
Branch analysis from position: 115
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 115
Branch analysis from position: 27
filename:       /in/8CDjX
function name:  produceDateString
number of ops:  120
compiled vars:  !0 = $dates, !1 = $ranges, !2 = $currentRange, !3 = $date, !4 = $output, !5 = $previous, !6 = $range, !7 = $format
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   15     1        INIT_FCALL                                               'sort'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                                 
   18     4        ASSIGN                                                   !1, <array>
   19     5        ASSIGN                                                   !2, <array>
   20     6      > FE_RESET_R                                       $11     !0, ->27
          7    > > FE_FETCH_R                                               $11, !3, ->27
   21     8    >   ISSET_ISEMPTY_CV                                 ~12     !2
          9      > JMPNZ_EX                                         ~12     ~12, ->18
         10    >   INIT_FCALL_BY_NAME                                       'consecutive'
         11        INIT_FCALL                                               'end'
         12        SEND_REF                                                 !2
         13        DO_ICALL                                         $13     
         14        SEND_VAR_NO_REF_EX                                       $13
         15        SEND_VAR_EX                                              !3
         16        DO_FCALL                                      0  $14     
         17        BOOL                                             ~12     $14
         18    > > JMPZ                                                     ~12, ->22
   22    19    >   ASSIGN_DIM                                               !2
         20        OP_DATA                                                  !3
   21    21      > JMP                                                      ->26
   24    22    >   ASSIGN_DIM                                               !1
         23        OP_DATA                                                  !2
   25    24        INIT_ARRAY                                       ~17     !3
         25        ASSIGN                                                   !2, ~17
   20    26    > > JMP                                                      ->7
         27    >   FE_FREE                                                  $11
   28    28        ASSIGN_DIM                                               !1
         29        OP_DATA                                                  !2
   31    30        ASSIGN                                                   !4, ''
   32    31        ASSIGN                                                   !5, null
   33    32      > FE_RESET_R                                       $22     !1, ->115
         33    > > FE_FETCH_R                                               $22, !6, ->115
   35    34    >   ISSET_ISEMPTY_CV                                 ~23     !4
         35        BOOL_NOT                                         ~24     ~23
         36      > JMPZ                                                     ~24, ->38
   36    37    >   ASSIGN_OP                                     8          !4, '%2C+'
   42    38    >   TYPE_CHECK                                    2  ~26     !5
         39      > JMPNZ_EX                                         ~26     ~26, ->54
         40    >   INIT_FCALL                                               'end'
         41        SEND_REF                                                 !5
         42        DO_ICALL                                         $27     
         43        INIT_METHOD_CALL                                         $27, 'format'
         44        SEND_VAL_EX                                              'm'
         45        DO_FCALL                                      0  $28     
         46        INIT_FCALL                                               'reset'
         47        SEND_REF                                                 !6
         48        DO_ICALL                                         $29     
         49        INIT_METHOD_CALL                                         $29, 'format'
         50        SEND_VAL_EX                                              'm'
         51        DO_FCALL                                      0  $30     
         52        IS_NOT_IDENTICAL                                 ~31     $28, $30
         53        BOOL                                             ~26     ~31
         54    > > JMPZ                                                     ~26, ->57
   43    55    >   QM_ASSIGN                                        ~32     'M.+j'
         56      > JMP                                                      ->58
   44    57    >   QM_ASSIGN                                        ~32     'j'
   42    58    >   ASSIGN                                                   !7, ~32
   47    59        COUNT                                            ~34     !6
         60        IS_SMALLER                                               1, ~34
         61      > JMPZ                                                     ~35, ->106
   49    62    >   INIT_FCALL_BY_NAME                                       'sameMonth'
         63        INIT_FCALL                                               'reset'
         64        SEND_REF                                                 !6
         65        DO_ICALL                                         $36     
         66        SEND_VAR_NO_REF_EX                                       $36
         67        INIT_FCALL                                               'end'
         68        SEND_REF                                                 !6
         69        DO_ICALL                                         $37     
         70        SEND_VAR_NO_REF_EX                                       $37
         71        DO_FCALL                                      0  $38     
         72      > JMPZ                                                     $38, ->89
   50    73    >   INIT_FCALL                                               'reset'
         74        SEND_REF                                                 !6
         75        DO_ICALL                                         $39     
         76        INIT_METHOD_CALL                                         $39, 'format'
         77        SEND_VAR_EX                                              !7
         78        DO_FCALL                                      0  $40     
         79        CONCAT                                           ~41     $40, '-'
         80        INIT_FCALL                                               'end'
         81        SEND_REF                                                 !6
         82        DO_ICALL                                         $42     
         83        INIT_METHOD_CALL                                         $42, 'format'
         84        SEND_VAL_EX                                              'j'
         85        DO_FCALL                                      0  $43     
         86        CONCAT                                           ~44     ~41, $43
         87        QM_ASSIGN                                        ~45     ~44
         88      > JMP                                                      ->104
   51    89    >   INIT_FCALL                                               'reset'
         90        SEND_REF                                                 !6
         91        DO_ICALL                                         $46     
         92        INIT_METHOD_CALL                                         $46, 'format'
         93        SEND_VAL_EX                                              'M.+j'
         94        DO_FCALL                                      0  $47     
         95        CONCAT                                           ~48     $47, '-'
         96        INIT_FCALL                                               'end'
         97        SEND_REF                                                 !6
         98        DO_ICALL                                         $49     
         99        INIT_METHOD_CALL                                         $49, 'format'
        100        SEND_VAL_EX                                              'M.+j'
        101        DO_FCALL                                      0  $50     
        102        CONCAT                                           ~51     ~48, $50
        103        QM_ASSIGN                                        ~45     ~51
        104    >   ASSIGN_OP                                     8          !4, ~45
   47   105      > JMP                                                      ->113
   53   106    >   INIT_FCALL                                               'reset'
        107        SEND_REF                                                 !6
        108        DO_ICALL                                         $53     
        109        INIT_METHOD_CALL                                         $53, 'format'
        110        SEND_VAR_EX                                              !7
        111        DO_FCALL                                      0  $54     
        112        ASSIGN_OP                                     8          !4, $54
   56   113    >   ASSIGN                                                   !5, !6
   33   114      > JMP                                                      ->33
        115    >   FE_FREE                                                  $22
   59   116        VERIFY_RETURN_TYPE                                       !4
        117      > RETURN                                                   !4
   60   118*       VERIFY_RETURN_TYPE                                       
        119*     > RETURN                                                   null

End of function producedatestring

Function consecutive:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8CDjX
function name:  consecutive
number of ops:  27
compiled vars:  !0 = $t1, !1 = $t2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   64     2        INIT_METHOD_CALL                                         !0, 'setTime'
          3        SEND_VAL_EX                                              0
          4        SEND_VAL_EX                                              0
          5        SEND_VAL_EX                                              0
          6        SEND_VAL_EX                                              0
          7        DO_FCALL                                      0          
   65     8        INIT_METHOD_CALL                                         !1, 'setTime'
          9        SEND_VAL_EX                                              0
         10        SEND_VAL_EX                                              0
         11        SEND_VAL_EX                                              0
         12        SEND_VAL_EX                                              0
         13        DO_FCALL                                      0          
   66    14        INIT_FCALL                                               'abs'
         15        INIT_METHOD_CALL                                         !1, 'getTimestamp'
         16        DO_FCALL                                      0  $4      
         17        INIT_METHOD_CALL                                         !0, 'getTimestamp'
         18        DO_FCALL                                      0  $5      
         19        SUB                                              ~6      $4, $5
         20        SEND_VAL                                                 ~6
         21        DO_ICALL                                         $7      
         22        IS_SMALLER                                       ~8      $7, 87000
         23        VERIFY_RETURN_TYPE                                       ~8
         24      > RETURN                                                   ~8
   67    25*       VERIFY_RETURN_TYPE                                       
         26*     > RETURN                                                   null

End of function consecutive

Function samemonth:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8CDjX
function name:  sameMonth
number of ops:  13
compiled vars:  !0 = $t1, !1 = $t2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   69     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   71     2        INIT_METHOD_CALL                                         !0, 'format'
          3        SEND_VAL_EX                                              'Y-m'
          4        DO_FCALL                                      0  $2      
          5        INIT_METHOD_CALL                                         !1, 'format'
          6        SEND_VAL_EX                                              'Y-m'
          7        DO_FCALL                                      0  $3      
          8        IS_IDENTICAL                                     ~4      $2, $3
          9        VERIFY_RETURN_TYPE                                       ~4
         10      > RETURN                                                   ~4
   72    11*       VERIFY_RETURN_TYPE                                       
         12*     > RETURN                                                   null

End of function samemonth

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
128.92 ms | 1041 KiB | 18 Q