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 '.$test[2].' of '.$test[1].' yields '.date('r', align_timestamp_to($test[0], $test[1], $test[2])).'<br/>';
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 43, Position 2 = 72
Branch analysis from position: 43
2 jumps found. (Code = 78) Position 1 = 44, Position 2 = 72
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 72
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 72
filename:       /in/mGXld
function name:  (null)
number of ops:  74
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, ->72
         43    > > FE_FETCH_R                                               $14, !0, ->72
   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        FETCH_DIM_R                                      ~18     !0, 2
         51        CONCAT                                           ~19     ~17, ~18
         52        CONCAT                                           ~20     ~19, '+of+'
         53        FETCH_DIM_R                                      ~21     !0, 1
         54        CONCAT                                           ~22     ~20, ~21
         55        CONCAT                                           ~23     ~22, '+yields+'
         56        INIT_FCALL                                               'date'
         57        SEND_VAL                                                 'r'
         58        INIT_FCALL                                               'align_timestamp_to'
         59        FETCH_DIM_R                                      ~24     !0, 0
         60        SEND_VAL                                                 ~24
         61        FETCH_DIM_R                                      ~25     !0, 1
         62        SEND_VAL                                                 ~25
         63        FETCH_DIM_R                                      ~26     !0, 2
         64        SEND_VAL                                                 ~26
         65        DO_FCALL                                      0  $27     
         66        SEND_VAR                                                 $27
         67        DO_ICALL                                         $28     
         68        CONCAT                                           ~29     ~23, $28
         69        CONCAT                                           ~30     ~29, '%3Cbr%2F%3E'
         70        ECHO                                                     ~30
   18    71      > JMP                                                      ->43
         72    >   FE_FREE                                                  $14
   24    73      > 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/mGXld
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:
146.67 ms | 1398 KiB | 18 Q