3v4l.org

run code in 500+ PHP versions simultaneously
<?php function timeBetween(string $dob): array { $DAYS_IN_YEAR = 365; $DAYS_IN_WEEK = 7; $now = new DateTime(); // now $birthDate = new DateTime($dob); $totalDays = $now->diff($birthDate)->days; return array( 'Year(s)' => (int)floor($totalDays/$DAYS_IN_YEAR), 'Week(s)' => (int)floor(($totalDays % $DAYS_IN_YEAR) / $DAYS_IN_WEEK), 'Day(s)' => (int)($totalDays % $DAYS_IN_YEAR) % $DAYS_IN_WEEK ); } function timeBetweenToString(array $timeBetween): string { $JOIN_PHRASE = ', and '; $timeBetween = array_filter($timeBetween); $result = array_reduce(array_keys($timeBetween), function ($results, $key) use ($timeBetween) { $value = $timeBetween[$key]; $results[] = "$value $key"; return $results; }, []); return implode($JOIN_PHRASE, $result); } $people = []; $people[] = array('name' => 'Bob', 'bday' => '2000-02-17'); $people[] = array('name' => 'Jane', 'bday' => '2005-03-30'); $people[] = array('name' => 'Mark', 'bday' => '2011-12-05'); foreach($people as $value) { echo "{$value['name']} was born " . timeBetweenToString(timeBetween($value['bday'])) . " ago\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 23
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 23
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/BL5MN
function name:  (null)
number of ops:  25
compiled vars:  !0 = $people, !1 = $value
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   32     0  E >   ASSIGN                                                       !0, <array>
   33     1        ASSIGN_DIM                                                   !0
          2        OP_DATA                                                      <array>
   34     3        ASSIGN_DIM                                                   !0
          4        OP_DATA                                                      <array>
   35     5        ASSIGN_DIM                                                   !0
          6        OP_DATA                                                      <array>
   37     7      > FE_RESET_R                                           $6      !0, ->23
          8    > > FE_FETCH_R                                                   $6, !1, ->23
   38     9    >   FETCH_DIM_R                                          ~7      !1, 'name'
         10        NOP                                                          
         11        FAST_CONCAT                                          ~8      ~7, '+was+born+'
         12        INIT_FCALL                                                   'timebetweentostring'
         13        INIT_FCALL                                                   'timebetween'
         14        FETCH_DIM_R                                          ~9      !1, 'bday'
         15        SEND_VAL                                                     ~9
         16        DO_FCALL                                          0  $10     
         17        SEND_VAR                                                     $10
         18        DO_FCALL                                          0  $11     
         19        CONCAT                                               ~12     ~8, $11
         20        CONCAT                                               ~13     ~12, '+ago%0A'
         21        ECHO                                                         ~13
   37    22      > JMP                                                          ->8
         23    >   FE_FREE                                                      $6
   39    24      > RETURN                                                       1

Function timebetween:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BL5MN
function name:  timeBetween
number of ops:  36
compiled vars:  !0 = $dob, !1 = $DAYS_IN_YEAR, !2 = $DAYS_IN_WEEK, !3 = $now, !4 = $birthDate, !5 = $totalDays
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
    4     1        ASSIGN                                                       !1, 365
    5     2        ASSIGN                                                       !2, 7
    7     3        NEW                                                  $8      'DateTime'
          4        DO_FCALL                                          0          
          5        ASSIGN                                                       !3, $8
    8     6        NEW                                                  $11     'DateTime'
          7        SEND_VAR_EX                                                  !0
          8        DO_FCALL                                          0          
          9        ASSIGN                                                       !4, $11
    9    10        INIT_METHOD_CALL                                             !3, 'diff'
         11        SEND_VAR_EX                                                  !4
         12        DO_FCALL                                          0  $14     
         13        FETCH_OBJ_R                                          ~15     $14, 'days'
         14        ASSIGN                                                       !5, ~15
   12    15        INIT_FCALL                                                   'floor'
         16        DIV                                                  ~17     !5, !1
         17        SEND_VAL                                                     ~17
         18        DO_ICALL                                             $18     
         19        CAST                                              4  ~19     $18
         20        INIT_ARRAY                                           ~20     ~19, 'Year%28s%29'
   13    21        INIT_FCALL                                                   'floor'
         22        MOD                                                  ~21     !5, !1
         23        DIV                                                  ~22     ~21, !2
         24        SEND_VAL                                                     ~22
         25        DO_ICALL                                             $23     
         26        CAST                                              4  ~24     $23
         27        ADD_ARRAY_ELEMENT                                    ~20     ~24, 'Week%28s%29'
   14    28        MOD                                                  ~25     !5, !1
         29        CAST                                              4  ~26     ~25
         30        MOD                                                  ~27     ~26, !2
         31        ADD_ARRAY_ELEMENT                                    ~20     ~27, 'Day%28s%29'
         32        VERIFY_RETURN_TYPE                                           ~20
         33      > RETURN                                                       ~20
   16    34*       VERIFY_RETURN_TYPE                                           
         35*     > RETURN                                                       null

End of function timebetween

Function timebetweentostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BL5MN
function name:  timeBetweenToString
number of ops:  22
compiled vars:  !0 = $timeBetween, !1 = $JOIN_PHRASE, !2 = $result
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   18     0  E >   RECV                                                 !0      
   19     1        ASSIGN                                                       !1, '%2C+and+'
   21     2        INIT_FCALL                                                   'array_filter'
          3        SEND_VAR                                                     !0
          4        DO_ICALL                                             $4      
          5        ASSIGN                                                       !0, $4
   23     6        INIT_FCALL                                                   'array_reduce'
          7        INIT_FCALL                                                   'array_keys'
          8        SEND_VAR                                                     !0
          9        DO_ICALL                                             $6      
         10        SEND_VAR                                                     $6
         11        DECLARE_LAMBDA_FUNCTION                              ~7      [0]
         12        BIND_LEXICAL                                                 ~7, !0
   28    13        SEND_VAL                                                     ~7
         14        SEND_VAL                                                     <array>
   23    15        DO_ICALL                                             $8      
         16        ASSIGN                                                       !2, $8
   29    17        FRAMELESS_ICALL_2                implode             ~10     !1, !2
         18        VERIFY_RETURN_TYPE                                           ~10
         19      > RETURN                                                       ~10
   30    20*       VERIFY_RETURN_TYPE                                           
         21*     > 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/BL5MN
function name:  {closure:timeBetweenToString():23}
number of ops:  12
compiled vars:  !0 = $results, !1 = $key, !2 = $timeBetween, !3 = $value
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   23     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        BIND_STATIC                                                  !2
   24     3        FETCH_DIM_R                                          ~4      !2, !1
          4        ASSIGN                                                       !3, ~4
   26     5        ROPE_INIT                                         3  ~8      !3
          6        ROPE_ADD                                          1  ~8      ~8, '+'
          7        ROPE_END                                          2  ~7      ~8, !1
          8        ASSIGN_DIM                                                   !0
          9        OP_DATA                                                      ~7
   27    10      > RETURN                                                       !0
   28    11*     > RETURN                                                       null

End of Dynamic Function 0

End of function timebetweentostring

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
172.26 ms | 1413 KiB | 19 Q