3v4l.org

run code in 300+ PHP versions simultaneously
<?php function isValidDate(string $date, string $format = 'm/d/Y', string $yearPadding = '19'): bool { $date = preg_replace_callback( '~^(\d+)(\D)(\d+)\2(\d{2}|\d{4})$~', fn($m) => sprintf('%02d/%02d/%d', $m[1], $m[3], str_pad($m[4], 4, $yearPadding, STR_PAD_LEFT)), $date ); // sanitize $d = DateTime::createFromFormat($format, $date); // attempt to parse return $d && $d->format($format) === $date; // check if formatted string is same as sanitized string } $tests = [ '1-1-1992', '1-1-92', '1-01-1992', '1-01-92', '01-1-1992', '01-1-92', '01-01-1992', '01-01/92', '1/1/1992', '1/1/92', '1/01/1992', '1/01/92', '01/1/199', '01/1/92', '01/01/1992', '01/01/92' ]; var_export(array_map('isValidDate', $tests));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s8J7Z
function name:  (null)
number of ops:  9
compiled vars:  !0 = $tests
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   ASSIGN                                                   !0, <array>
   32     1        INIT_FCALL                                               'var_export'
          2        INIT_FCALL                                               'array_map'
          3        SEND_VAL                                                 'isValidDate'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                                 
          8      > RETURN                                                   1

Function isvaliddate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 17, Position 2 = 22
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/s8J7Z
function name:  isValidDate
number of ops:  26
compiled vars:  !0 = $date, !1 = $format, !2 = $yearPadding, !3 = $d
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'm%2Fd%2FY'
          2        RECV_INIT                                        !2      '19'
    4     3        INIT_FCALL                                               'preg_replace_callback'
    5     4        SEND_VAL                                                 '%7E%5E%28%5Cd%2B%29%28%5CD%29%28%5Cd%2B%29%5C2%28%5Cd%7B2%7D%7C%5Cd%7B4%7D%29%24%7E'
    6     5        DECLARE_LAMBDA_FUNCTION                          ~4      [0]
          6        BIND_LEXICAL                                             ~4, !2
          7        SEND_VAL                                                 ~4
    7     8        SEND_VAR                                                 !0
    4     9        DO_ICALL                                         $5      
         10        ASSIGN                                                   !0, $5
    9    11        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         12        SEND_VAR                                                 !1
         13        SEND_VAR                                                 !0
         14        DO_FCALL                                      0  $7      
         15        ASSIGN                                                   !3, $7
   10    16      > JMPZ_EX                                          ~9      !3, ->22
         17    >   INIT_METHOD_CALL                                         !3, 'format'
         18        SEND_VAR_EX                                              !1
         19        DO_FCALL                                      0  $10     
         20        IS_IDENTICAL                                     ~11     !0, $10
         21        BOOL                                             ~9      ~11
         22    >   VERIFY_RETURN_TYPE                                       ~9
         23      > RETURN                                                   ~9
   11    24*       VERIFY_RETURN_TYPE                                       
         25*     > 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/s8J7Z
function name:  {closure}
number of ops:  19
compiled vars:  !0 = $m, !1 = $yearPadding
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        INIT_FCALL                                               'sprintf'
          3        SEND_VAL                                                 '%2502d%2F%2502d%2F%25d'
          4        FETCH_DIM_R                                      ~2      !0, 1
          5        SEND_VAL                                                 ~2
          6        FETCH_DIM_R                                      ~3      !0, 3
          7        SEND_VAL                                                 ~3
          8        INIT_FCALL                                               'str_pad'
          9        FETCH_DIM_R                                      ~4      !0, 4
         10        SEND_VAL                                                 ~4
         11        SEND_VAL                                                 4
         12        SEND_VAR                                                 !1
         13        SEND_VAL                                                 0
         14        DO_ICALL                                         $5      
         15        SEND_VAR                                                 $5
         16        DO_ICALL                                         $6      
         17      > RETURN                                                   $6
         18*     > RETURN                                                   null

End of Dynamic Function 0

End of function isvaliddate

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.74 ms | 1009 KiB | 18 Q