3v4l.org

run code in 300+ PHP versions simultaneously
<?php function align_timestamp_to($stamp, $unit, $past){ // timestamp, unit string, start/end static $modes = array( 'year' => 'last year', 'week' => 'last monday', 'month' => 'first day of this month', 'hour' => 'last hour', 'minute' => 'last minute', ); // TODO error out if unknown $unit $offset = $modes[$unit]; if($past == 'end')$offset = '+1 '.$unit.' '.$offset.' -1 second'; return strtotime($offset, $stamp); } foreach(array( array(strtotime('4 Apr 2012'), 'year', 'start'), array(strtotime('4 Apr 2012'), 'year', 'end'), array(strtotime('4 Apr 2012'), 'month', 'start'), array(strtotime('4 Apr 2012'), 'month', 'end'), array(strtotime('4 Apr 2012'), 'week', 'start'), array(strtotime('4 Apr 2012'), 'week', 'end'), ) as $test)echo date('r', $test[0]).' aligned '.str_pad($test[2], 5, ' ', STR_PAD_BOTH).' of '.$test[1].' yields '.date('r', align_timestamp_to($test[0], $test[1], $test[2])).PHP_EOL;
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 43, Position 2 = 78
Branch analysis from position: 43
2 jumps found. (Code = 78) Position 1 = 44, Position 2 = 78
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 78
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 78
filename:       /in/0NqJI
function name:  (null)
number of ops:  80
compiled vars:  !0 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   INIT_FCALL                                               'strtotime'
          1        SEND_VAL                                                 '4+Apr+2012'
          2        DO_ICALL                                         $1      
          3        INIT_ARRAY                                       ~2      $1
          4        ADD_ARRAY_ELEMENT                                ~2      'year'
          5        ADD_ARRAY_ELEMENT                                ~2      'start'
          6        INIT_ARRAY                                       ~3      ~2
   19     7        INIT_FCALL                                               'strtotime'
          8        SEND_VAL                                                 '4+Apr+2012'
          9        DO_ICALL                                         $4      
         10        INIT_ARRAY                                       ~5      $4
         11        ADD_ARRAY_ELEMENT                                ~5      'year'
         12        ADD_ARRAY_ELEMENT                                ~5      'end'
         13        ADD_ARRAY_ELEMENT                                ~3      ~5
   20    14        INIT_FCALL                                               'strtotime'
         15        SEND_VAL                                                 '4+Apr+2012'
         16        DO_ICALL                                         $6      
         17        INIT_ARRAY                                       ~7      $6
         18        ADD_ARRAY_ELEMENT                                ~7      'month'
         19        ADD_ARRAY_ELEMENT                                ~7      'start'
         20        ADD_ARRAY_ELEMENT                                ~3      ~7
   21    21        INIT_FCALL                                               'strtotime'
         22        SEND_VAL                                                 '4+Apr+2012'
         23        DO_ICALL                                         $8      
         24        INIT_ARRAY                                       ~9      $8
         25        ADD_ARRAY_ELEMENT                                ~9      'month'
         26        ADD_ARRAY_ELEMENT                                ~9      'end'
         27        ADD_ARRAY_ELEMENT                                ~3      ~9
   22    28        INIT_FCALL                                               'strtotime'
         29        SEND_VAL                                                 '4+Apr+2012'
         30        DO_ICALL                                         $10     
         31        INIT_ARRAY                                       ~11     $10
         32        ADD_ARRAY_ELEMENT                                ~11     'week'
         33        ADD_ARRAY_ELEMENT                                ~11     'start'
         34        ADD_ARRAY_ELEMENT                                ~3      ~11
   23    35        INIT_FCALL                                               'strtotime'
         36        SEND_VAL                                                 '4+Apr+2012'
         37        DO_ICALL                                         $12     
         38        INIT_ARRAY                                       ~13     $12
         39        ADD_ARRAY_ELEMENT                                ~13     'week'
         40        ADD_ARRAY_ELEMENT                                ~13     'end'
         41        ADD_ARRAY_ELEMENT                                ~3      ~13
         42      > FE_RESET_R                                       $14     ~3, ->78
         43    > > FE_FETCH_R                                               $14, !0, ->78
   24    44    >   INIT_FCALL                                               'date'
         45        SEND_VAL                                                 'r'
         46        FETCH_DIM_R                                      ~15     !0, 0
         47        SEND_VAL                                                 ~15
         48        DO_ICALL                                         $16     
         49        CONCAT                                           ~17     $16, '+aligned+'
         50        INIT_FCALL                                               'str_pad'
         51        FETCH_DIM_R                                      ~18     !0, 2
         52        SEND_VAL                                                 ~18
         53        SEND_VAL                                                 5
         54        SEND_VAL                                                 '+'
         55        SEND_VAL                                                 2
         56        DO_ICALL                                         $19     
         57        CONCAT                                           ~20     ~17, $19
         58        CONCAT                                           ~21     ~20, '+of+'
         59        FETCH_DIM_R                                      ~22     !0, 1
         60        CONCAT                                           ~23     ~21, ~22
         61        CONCAT                                           ~24     ~23, '+yields+'
         62        INIT_FCALL                                               'date'
         63        SEND_VAL                                                 'r'
         64        INIT_FCALL                                               'align_timestamp_to'
         65        FETCH_DIM_R                                      ~25     !0, 0
         66        SEND_VAL                                                 ~25
         67        FETCH_DIM_R                                      ~26     !0, 1
         68        SEND_VAL                                                 ~26
         69        FETCH_DIM_R                                      ~27     !0, 2
         70        SEND_VAL                                                 ~27
         71        DO_FCALL                                      0  $28     
         72        SEND_VAR                                                 $28
         73        DO_ICALL                                         $29     
         74        CONCAT                                           ~30     ~24, $29
         75        CONCAT                                           ~31     ~30, '%0A'
         76        ECHO                                                     ~31
   18    77      > JMP                                                      ->43
         78    >   FE_FREE                                                  $14
   24    79      > RETURN                                                   1

Function align_timestamp_to:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/0NqJI
function name:  align_timestamp_to
number of ops:  19
compiled vars:  !0 = $stamp, !1 = $unit, !2 = $past, !3 = $modes, !4 = $offset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
    4     3        BIND_STATIC                                              !3
   12     4        FETCH_DIM_R                                      ~5      !3, !1
          5        ASSIGN                                                   !4, ~5
   13     6        IS_EQUAL                                                 !2, 'end'
          7      > JMPZ                                                     ~7, ->13
          8    >   CONCAT                                           ~8      '%2B1+', !1
          9        CONCAT                                           ~9      ~8, '+'
         10        CONCAT                                           ~10     ~9, !4
         11        CONCAT                                           ~11     ~10, '+-1+second'
         12        ASSIGN                                                   !4, ~11
   14    13    >   INIT_FCALL                                               'strtotime'
         14        SEND_VAR                                                 !4
         15        SEND_VAR                                                 !0
         16        DO_ICALL                                         $13     
         17      > RETURN                                                   $13
   15    18*     > RETURN                                                   null

End of function align_timestamp_to

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.77 ms | 1406 KiB | 20 Q