3v4l.org

run code in 300+ PHP versions simultaneously
<?php //helper function to pluralize words function pluralize( $count, $text ){ return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) ); } /** * Difference of dates * @param type $date1 DateTime first date * @param type $date2 DateTime second date * @return type string year, month, day, hour, minute, second */ function timeDiff($date1, $date2 ){ $string = ""; $interval =$date1->diff($date2); $suffix = ( $interval->invert ? ' ago' : '' ); if ( $v = $interval->y >= 1 ) $string .= pluralize( $interval->y, 'year' ). ', '; if ( $v = $interval->m >= 1 ) $string .= pluralize( $interval->m, 'month' ). ', '; if ( $v = $interval->d >= 1 ) $string .= pluralize( $interval->d, 'day' ). ', '; if ( $v = $interval->h >= 1 ) $string .= pluralize( ($interval->h), 'hour' ) . ', '; if ( $v = $interval->i >= 1 ) $string .= pluralize( $interval->i, 'minute' ). ', '; if ( $v = $interval->i >= 1 ) $string .= pluralize( $interval->s, 'second' ). ' '; return $string . $suffix; } $date1 = new DateTime("2014-05-10 20:00:00"); $date2 = new DateTime("2015-11-20 19:45:00"); //This should produce "1 year, 6 months, 9 days, 23 hours, 45 minutes" //But it does produce "1 year, 6 months, 10 days, 45 minutes" echo timeDiff($date1, $date2); $date3 = new DateTime("2015-11-20 20:00:00"); echo "\n"; //This should, and does produce "1 year, 6 months, 10 days" echo timeDiff($date1, $date3);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Kp26c
function name:  (null)
number of ops:  24
compiled vars:  !0 = $date1, !1 = $date2, !2 = $date3
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   NEW                                              $3      'DateTime'
          1        SEND_VAL_EX                                              '2014-05-10+20%3A00%3A00'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $3
   29     4        NEW                                              $6      'DateTime'
          5        SEND_VAL_EX                                              '2015-11-20+19%3A45%3A00'
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !1, $6
   33     8        INIT_FCALL                                               'timediff'
          9        SEND_VAR                                                 !0
         10        SEND_VAR                                                 !1
         11        DO_FCALL                                      0  $9      
         12        ECHO                                                     $9
   35    13        NEW                                              $10     'DateTime'
         14        SEND_VAL_EX                                              '2015-11-20+20%3A00%3A00'
         15        DO_FCALL                                      0          
         16        ASSIGN                                                   !2, $10
   36    17        ECHO                                                     '%0A'
   38    18        INIT_FCALL                                               'timediff'
         19        SEND_VAR                                                 !0
         20        SEND_VAR                                                 !2
         21        DO_FCALL                                      0  $13     
         22        ECHO                                                     $13
         23      > RETURN                                                   1

Function pluralize:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Kp26c
function name:  pluralize
number of ops:  15
compiled vars:  !0 = $count, !1 = $text
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        IS_EQUAL                                                 !0, 1
          3      > JMPZ                                                     ~2, ->8
          4    >   NOP                                                      
          5        FAST_CONCAT                                      ~3      '+', !1
          6        QM_ASSIGN                                        ~4      ~3
          7      > JMP                                                      ->12
          8    >   ROPE_INIT                                     3  ~6      '+'
          9        ROPE_ADD                                      1  ~6      ~6, !1
         10        ROPE_END                                      2  ~5      ~6, 's'
         11        QM_ASSIGN                                        ~4      ~5
         12    >   CONCAT                                           ~8      !0, ~4
         13      > RETURN                                                   ~8
    6    14*     > RETURN                                                   null

End of function pluralize

Function timediff:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 24
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 35
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 46
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 57
Branch analysis from position: 50
2 jumps found. (Code = 43) Position 1 = 61, Position 2 = 68
Branch analysis from position: 61
2 jumps found. (Code = 43) Position 1 = 72, Position 2 = 79
Branch analysis from position: 72
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 79
Branch analysis from position: 68
Branch analysis from position: 57
Branch analysis from position: 46
Branch analysis from position: 35
Branch analysis from position: 24
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 24
Branch analysis from position: 17
Branch analysis from position: 24
filename:       /in/Kp26c
function name:  timeDiff
number of ops:  82
compiled vars:  !0 = $date1, !1 = $date2, !2 = $string, !3 = $interval, !4 = $suffix, !5 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   15     2        ASSIGN                                                   !2, ''
   16     3        INIT_METHOD_CALL                                         !0, 'diff'
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0  $7      
          6        ASSIGN                                                   !3, $7
   17     7        FETCH_OBJ_R                                      ~9      !3, 'invert'
          8      > JMPZ                                                     ~9, ->11
          9    >   QM_ASSIGN                                        ~10     '+ago'
         10      > JMP                                                      ->12
         11    >   QM_ASSIGN                                        ~10     ''
         12    >   ASSIGN                                                   !4, ~10
   18    13        FETCH_OBJ_R                                      ~12     !3, 'y'
         14        IS_SMALLER_OR_EQUAL                              ~13     1, ~12
         15        ASSIGN                                           ~14     !5, ~13
         16      > JMPZ                                                     ~14, ->24
         17    >   INIT_FCALL                                               'pluralize'
         18        FETCH_OBJ_R                                      ~15     !3, 'y'
         19        SEND_VAL                                                 ~15
         20        SEND_VAL                                                 'year'
         21        DO_FCALL                                      0  $16     
         22        CONCAT                                           ~17     $16, '%2C+'
         23        ASSIGN_OP                                     8          !2, ~17
   19    24    >   FETCH_OBJ_R                                      ~19     !3, 'm'
         25        IS_SMALLER_OR_EQUAL                              ~20     1, ~19
         26        ASSIGN                                           ~21     !5, ~20
         27      > JMPZ                                                     ~21, ->35
         28    >   INIT_FCALL                                               'pluralize'
         29        FETCH_OBJ_R                                      ~22     !3, 'm'
         30        SEND_VAL                                                 ~22
         31        SEND_VAL                                                 'month'
         32        DO_FCALL                                      0  $23     
         33        CONCAT                                           ~24     $23, '%2C+'
         34        ASSIGN_OP                                     8          !2, ~24
   20    35    >   FETCH_OBJ_R                                      ~26     !3, 'd'
         36        IS_SMALLER_OR_EQUAL                              ~27     1, ~26
         37        ASSIGN                                           ~28     !5, ~27
         38      > JMPZ                                                     ~28, ->46
         39    >   INIT_FCALL                                               'pluralize'
         40        FETCH_OBJ_R                                      ~29     !3, 'd'
         41        SEND_VAL                                                 ~29
         42        SEND_VAL                                                 'day'
         43        DO_FCALL                                      0  $30     
         44        CONCAT                                           ~31     $30, '%2C+'
         45        ASSIGN_OP                                     8          !2, ~31
   21    46    >   FETCH_OBJ_R                                      ~33     !3, 'h'
         47        IS_SMALLER_OR_EQUAL                              ~34     1, ~33
         48        ASSIGN                                           ~35     !5, ~34
         49      > JMPZ                                                     ~35, ->57
         50    >   INIT_FCALL                                               'pluralize'
         51        FETCH_OBJ_R                                      ~36     !3, 'h'
         52        SEND_VAL                                                 ~36
         53        SEND_VAL                                                 'hour'
         54        DO_FCALL                                      0  $37     
         55        CONCAT                                           ~38     $37, '%2C+'
         56        ASSIGN_OP                                     8          !2, ~38
   22    57    >   FETCH_OBJ_R                                      ~40     !3, 'i'
         58        IS_SMALLER_OR_EQUAL                              ~41     1, ~40
         59        ASSIGN                                           ~42     !5, ~41
         60      > JMPZ                                                     ~42, ->68
         61    >   INIT_FCALL                                               'pluralize'
         62        FETCH_OBJ_R                                      ~43     !3, 'i'
         63        SEND_VAL                                                 ~43
         64        SEND_VAL                                                 'minute'
         65        DO_FCALL                                      0  $44     
         66        CONCAT                                           ~45     $44, '%2C+'
         67        ASSIGN_OP                                     8          !2, ~45
   23    68    >   FETCH_OBJ_R                                      ~47     !3, 'i'
         69        IS_SMALLER_OR_EQUAL                              ~48     1, ~47
         70        ASSIGN                                           ~49     !5, ~48
         71      > JMPZ                                                     ~49, ->79
         72    >   INIT_FCALL                                               'pluralize'
         73        FETCH_OBJ_R                                      ~50     !3, 's'
         74        SEND_VAL                                                 ~50
         75        SEND_VAL                                                 'second'
         76        DO_FCALL                                      0  $51     
         77        CONCAT                                           ~52     $51, '+'
         78        ASSIGN_OP                                     8          !2, ~52
   24    79    >   CONCAT                                           ~54     !2, !4
         80      > RETURN                                                   ~54
   25    81*     > RETURN                                                   null

End of function timediff

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.92 ms | 1011 KiB | 21 Q