3v4l.org

run code in 300+ PHP versions simultaneously
<?php date_default_timezone_set('UTC'); function dateDiff($startdate, $enddate) { $first = $startdate < $enddate ? $startdate : $enddate; $second = $startdate < $enddate ? $enddate : $startdate; $inbetween = $second - $first; $firsttemp = $first; $months = 0; $days = 0; while(TRUE) { if($firsttemp + 86400* date('t', $firsttemp) <= $second) { $months += 1; $firsttemp += 86400 * date('t', $firsttemp); } else { $days = floor(($second - $firsttemp) / 86400); break; } } $years = floor($months / 12); $months = $months % 12; $hours = floor($inbetween / 60 / 60 % 24); $minutes = floor($inbetween / 60 % 60); $seconds = $inbetween % 60; $dateObj = new stdClass(); $dateObj->years = $years; $dateObj->months = $months; $dateObj->days = $days; $dateObj->hours = $hours; $dateObj->minutes = $minutes; $dateObj->seconds = $seconds; $dateObj->dayyyy = floor($inbetween / 60 / 60 / 24); return $dateObj; } $str1 = "1970-01-01 + 21 sec"; $str2 = "2009-01-01 + 54612 sec"; $tempdate1 = $str1; $date1 = new DateTime($str1); $n = 0; $date2 = new DateTime($str2); $interval = $date1->diff($date2); var_dump($interval); $string1 = "(" . $interval->days . ") difference " . $interval->y . " years, " . $interval->m . " months, " . $interval->d . " days"; $mydate = dateDiff(strtotime($str1), strtotime($str2)); $string2 = "(" . $mydate->dayyyy . ") difference " . $mydate->years . " years, " . $mydate->months . " months, " . $mydate->days . " days, " . $mydate->hours . " hours, " . $mydate->minutes . " minutes, and " . $mydate->seconds . " seconds"; if($string1 !== $string2) { echo "<span style=\"color:red;\">", $string1, " - ", $string2, "</span><br />"; // This will never run } else { echo "<span style=\"color:green;\">", $string1, " - ", $string2, "</span><br />"; } $n++; $tempdate1 = $str1 . " +" . $n . " days";
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 70, Position 2 = 76
Branch analysis from position: 70
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 76
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/P7W7X
function name:  (null)
number of ops:  87
compiled vars:  !0 = $str1, !1 = $str2, !2 = $tempdate1, !3 = $date1, !4 = $n, !5 = $date2, !6 = $interval, !7 = $string1, !8 = $mydate, !9 = $string2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'date_default_timezone_set'
          1        SEND_VAL                                                 'UTC'
          2        DO_ICALL                                                 
   44     3        ASSIGN                                                   !0, '1970-01-01+%2B+21+sec'
   45     4        ASSIGN                                                   !1, '2009-01-01+%2B+54612+sec'
   47     5        ASSIGN                                                   !2, !0
   48     6        NEW                                              $14     'DateTime'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !3, $14
   49    10        ASSIGN                                                   !4, 0
   51    11        NEW                                              $18     'DateTime'
         12        SEND_VAR_EX                                              !1
         13        DO_FCALL                                      0          
         14        ASSIGN                                                   !5, $18
   52    15        INIT_METHOD_CALL                                         !3, 'diff'
         16        SEND_VAR_EX                                              !5
         17        DO_FCALL                                      0  $21     
         18        ASSIGN                                                   !6, $21
   53    19        INIT_FCALL                                               'var_dump'
         20        SEND_VAR                                                 !6
         21        DO_ICALL                                                 
   54    22        FETCH_OBJ_R                                      ~24     !6, 'days'
         23        CONCAT                                           ~25     '%28', ~24
         24        CONCAT                                           ~26     ~25, '%29+difference+'
         25        FETCH_OBJ_R                                      ~27     !6, 'y'
         26        CONCAT                                           ~28     ~26, ~27
         27        CONCAT                                           ~29     ~28, '+years%2C+'
         28        FETCH_OBJ_R                                      ~30     !6, 'm'
         29        CONCAT                                           ~31     ~29, ~30
         30        CONCAT                                           ~32     ~31, '+months%2C+'
         31        FETCH_OBJ_R                                      ~33     !6, 'd'
         32        CONCAT                                           ~34     ~32, ~33
         33        CONCAT                                           ~35     ~34, '+days'
         34        ASSIGN                                                   !7, ~35
   56    35        INIT_FCALL                                               'datediff'
         36        INIT_FCALL                                               'strtotime'
         37        SEND_VAR                                                 !0
         38        DO_ICALL                                         $37     
         39        SEND_VAR                                                 $37
         40        INIT_FCALL                                               'strtotime'
         41        SEND_VAR                                                 !1
         42        DO_ICALL                                         $38     
         43        SEND_VAR                                                 $38
         44        DO_FCALL                                      0  $39     
         45        ASSIGN                                                   !8, $39
   57    46        FETCH_OBJ_R                                      ~41     !8, 'dayyyy'
         47        CONCAT                                           ~42     '%28', ~41
         48        CONCAT                                           ~43     ~42, '%29+difference+'
         49        FETCH_OBJ_R                                      ~44     !8, 'years'
         50        CONCAT                                           ~45     ~43, ~44
         51        CONCAT                                           ~46     ~45, '+years%2C+'
         52        FETCH_OBJ_R                                      ~47     !8, 'months'
         53        CONCAT                                           ~48     ~46, ~47
         54        CONCAT                                           ~49     ~48, '+months%2C+'
         55        FETCH_OBJ_R                                      ~50     !8, 'days'
         56        CONCAT                                           ~51     ~49, ~50
         57        CONCAT                                           ~52     ~51, '+days%2C+'
         58        FETCH_OBJ_R                                      ~53     !8, 'hours'
         59        CONCAT                                           ~54     ~52, ~53
         60        CONCAT                                           ~55     ~54, '+hours%2C+'
         61        FETCH_OBJ_R                                      ~56     !8, 'minutes'
         62        CONCAT                                           ~57     ~55, ~56
         63        CONCAT                                           ~58     ~57, '+minutes%2C+and+'
         64        FETCH_OBJ_R                                      ~59     !8, 'seconds'
         65        CONCAT                                           ~60     ~58, ~59
         66        CONCAT                                           ~61     ~60, '+seconds'
         67        ASSIGN                                                   !9, ~61
   59    68        IS_NOT_IDENTICAL                                         !7, !9
         69      > JMPZ                                                     ~63, ->76
   60    70    >   ECHO                                                     '%3Cspan+style%3D%22color%3Ared%3B%22%3E'
         71        ECHO                                                     !7
         72        ECHO                                                     '+-+'
         73        ECHO                                                     !9
         74        ECHO                                                     '%3C%2Fspan%3E%3Cbr+%2F%3E'
         75      > JMP                                                      ->81
   62    76    >   ECHO                                                     '%3Cspan+style%3D%22color%3Agreen%3B%22%3E'
         77        ECHO                                                     !7
         78        ECHO                                                     '+-+'
         79        ECHO                                                     !9
         80        ECHO                                                     '%3C%2Fspan%3E%3Cbr+%2F%3E'
   65    81    >   PRE_INC                                                  !4
   66    82        CONCAT                                           ~65     !0, '+%2B'
         83        CONCAT                                           ~66     ~65, !4
         84        CONCAT                                           ~67     ~66, '+days'
         85        ASSIGN                                                   !2, ~67
         86      > RETURN                                                   1

Function datediff:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
2 jumps found. (Code = 44) Position 1 = 44, Position 2 = 20
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 36
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
Branch analysis from position: 12
filename:       /in/P7W7X
function name:  dateDiff
number of ops:  91
compiled vars:  !0 = $startdate, !1 = $enddate, !2 = $first, !3 = $second, !4 = $inbetween, !5 = $firsttemp, !6 = $months, !7 = $days, !8 = $years, !9 = $hours, !10 = $minutes, !11 = $seconds, !12 = $dateObj
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    6     2        IS_SMALLER                                               !0, !1
          3      > JMPZ                                                     ~13, ->6
          4    >   QM_ASSIGN                                        ~14     !0
          5      > JMP                                                      ->7
          6    >   QM_ASSIGN                                        ~14     !1
          7    >   ASSIGN                                                   !2, ~14
    7     8        IS_SMALLER                                               !0, !1
          9      > JMPZ                                                     ~16, ->12
         10    >   QM_ASSIGN                                        ~17     !1
         11      > JMP                                                      ->13
         12    >   QM_ASSIGN                                        ~17     !0
         13    >   ASSIGN                                                   !3, ~17
    8    14        SUB                                              ~19     !3, !2
         15        ASSIGN                                                   !4, ~19
    9    16        ASSIGN                                                   !5, !2
   11    17        ASSIGN                                                   !6, 0
   12    18        ASSIGN                                                   !7, 0
   14    19      > JMP                                                      ->43
   15    20    >   INIT_FCALL                                               'date'
         21        SEND_VAL                                                 't'
         22        SEND_VAR                                                 !5
         23        DO_ICALL                                         $24     
         24        MUL                                              ~25     $24, 86400
         25        ADD                                              ~26     !5, ~25
         26        IS_SMALLER_OR_EQUAL                                      ~26, !3
         27      > JMPZ                                                     ~27, ->36
   16    28    >   ASSIGN_OP                                     1          !6, 1
   17    29        INIT_FCALL                                               'date'
         30        SEND_VAL                                                 't'
         31        SEND_VAR                                                 !5
         32        DO_ICALL                                         $29     
         33        MUL                                              ~30     $29, 86400
         34        ASSIGN_OP                                     1          !5, ~30
         35      > JMP                                                      ->43
   19    36    >   INIT_FCALL                                               'floor'
         37        SUB                                              ~32     !3, !5
         38        DIV                                              ~33     ~32, 86400
         39        SEND_VAL                                                 ~33
         40        DO_ICALL                                         $34     
         41        ASSIGN                                                   !7, $34
   20    42      > JMP                                                      ->44
   14    43    > > JMPNZ                                                    <true>, ->20
   24    44    >   INIT_FCALL                                               'floor'
         45        DIV                                              ~36     !6, 12
         46        SEND_VAL                                                 ~36
         47        DO_ICALL                                         $37     
         48        ASSIGN                                                   !8, $37
   25    49        MOD                                              ~39     !6, 12
         50        ASSIGN                                                   !6, ~39
   27    51        INIT_FCALL                                               'floor'
         52        DIV                                              ~41     !4, 60
         53        DIV                                              ~42     ~41, 60
         54        MOD                                              ~43     ~42, 24
         55        SEND_VAL                                                 ~43
         56        DO_ICALL                                         $44     
         57        ASSIGN                                                   !9, $44
   28    58        INIT_FCALL                                               'floor'
         59        DIV                                              ~46     !4, 60
         60        MOD                                              ~47     ~46, 60
         61        SEND_VAL                                                 ~47
         62        DO_ICALL                                         $48     
         63        ASSIGN                                                   !10, $48
   29    64        MOD                                              ~50     !4, 60
         65        ASSIGN                                                   !11, ~50
   31    66        NEW                                              $52     'stdClass'
         67        DO_FCALL                                      0          
         68        ASSIGN                                                   !12, $52
   33    69        ASSIGN_OBJ                                               !12, 'years'
         70        OP_DATA                                                  !8
   34    71        ASSIGN_OBJ                                               !12, 'months'
         72        OP_DATA                                                  !6
   35    73        ASSIGN_OBJ                                               !12, 'days'
         74        OP_DATA                                                  !7
   36    75        ASSIGN_OBJ                                               !12, 'hours'
         76        OP_DATA                                                  !9
   37    77        ASSIGN_OBJ                                               !12, 'minutes'
         78        OP_DATA                                                  !10
   38    79        ASSIGN_OBJ                                               !12, 'seconds'
         80        OP_DATA                                                  !11
   39    81        INIT_FCALL                                               'floor'
         82        DIV                                              ~62     !4, 60
         83        DIV                                              ~63     ~62, 60
         84        DIV                                              ~64     ~63, 24
         85        SEND_VAL                                                 ~64
         86        DO_ICALL                                         $65     
         87        ASSIGN_OBJ                                               !12, 'dayyyy'
         88        OP_DATA                                                  $65
   41    89      > RETURN                                                   !12
   42    90*     > RETURN                                                   null

End of function datediff

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
167.96 ms | 1414 KiB | 24 Q