3v4l.org

run code in 300+ PHP versions simultaneously
<?php function is_valid_date($value, $format = 'dd.mm.yyyy'){ if(strlen($value) >= 6 && strlen($format) == 10){ // find separator. Remove all other characters from $format $separator_only = str_replace(array('m','d','y'),'', $format); $separator = $separator_only[0]; // separator is first character if($separator && strlen($separator_only) == 2){ // make regex $regexp = str_replace('mm', '(0?[1-9]|1[0-2])', $format); $regexp = str_replace('dd', '(0?[1-9]|[1-2][0-9]|3[0-1])', $regexp); $regexp = str_replace('yyyy', '(19|20)?[0-9][0-9]', $regexp); $regexp = str_replace($separator, "\\" . $separator, $regexp); if($regexp != $value && preg_match('/'.$regexp.'\z/', $value)){ // check date $arr=explode($separator,$value); $day=$arr[0]; $month=$arr[1]; $year=$arr[2]; if(@checkdate($month, $day, $year)) return true; } } } return false; } var_dump(is_date('22.22.2222', 'mm.dd.yyyy')); // returns false var_dump(is_date('11/30/2008', 'mm/dd/yyyy')); // returns true var_dump(is_date('30-01-2008', 'dd-mm-yyyy')); // returns true var_dump(is_date('2008 01 30', 'yyyy mm dd')); // returns true
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vvXqN
function name:  (null)
number of ops:  29
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL_BY_NAME                                       'is_date'
          2        SEND_VAL_EX                                              '22.22.2222'
          3        SEND_VAL_EX                                              'mm.dd.yyyy'
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
   31     7        INIT_FCALL                                               'var_dump'
          8        INIT_FCALL_BY_NAME                                       'is_date'
          9        SEND_VAL_EX                                              '11%2F30%2F2008'
         10        SEND_VAL_EX                                              'mm%2Fdd%2Fyyyy'
         11        DO_FCALL                                      0  $2      
         12        SEND_VAR                                                 $2
         13        DO_ICALL                                                 
   32    14        INIT_FCALL                                               'var_dump'
         15        INIT_FCALL_BY_NAME                                       'is_date'
         16        SEND_VAL_EX                                              '30-01-2008'
         17        SEND_VAL_EX                                              'dd-mm-yyyy'
         18        DO_FCALL                                      0  $4      
         19        SEND_VAR                                                 $4
         20        DO_ICALL                                                 
   33    21        INIT_FCALL                                               'var_dump'
         22        INIT_FCALL_BY_NAME                                       'is_date'
         23        SEND_VAL_EX                                              '2008+01+30'
         24        SEND_VAL_EX                                              'yyyy+mm+dd'
         25        DO_FCALL                                      0  $6      
         26        SEND_VAR                                                 $6
         27        DO_ICALL                                                 
         28      > RETURN                                                   1

Function is_valid_date:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 77
Branch analysis from position: 9
2 jumps found. (Code = 46) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 77
Branch analysis from position: 22
2 jumps found. (Code = 46) Position 1 = 49, Position 2 = 56
Branch analysis from position: 49
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 77
Branch analysis from position: 57
2 jumps found. (Code = 43) Position 1 = 76, Position 2 = 77
Branch analysis from position: 76
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 77
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 77
Branch analysis from position: 56
Branch analysis from position: 77
Branch analysis from position: 21
Branch analysis from position: 77
Branch analysis from position: 8
filename:       /in/vvXqN
function name:  is_valid_date
number of ops:  79
compiled vars:  !0 = $value, !1 = $format, !2 = $separator_only, !3 = $separator, !4 = $regexp, !5 = $arr, !6 = $day, !7 = $month, !8 = $year
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'dd.mm.yyyy'
    3     2        STRLEN                                           ~9      !0
          3        IS_SMALLER_OR_EQUAL                              ~10     6, ~9
          4      > JMPZ_EX                                          ~10     ~10, ->8
          5    >   STRLEN                                           ~11     !1
          6        IS_EQUAL                                         ~12     ~11, 10
          7        BOOL                                             ~10     ~12
          8    > > JMPZ                                                     ~10, ->77
    6     9    >   INIT_FCALL                                               'str_replace'
         10        SEND_VAL                                                 <array>
         11        SEND_VAL                                                 ''
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                         $13     
         14        ASSIGN                                                   !2, $13
    7    15        FETCH_DIM_R                                      ~15     !2, 0
         16        ASSIGN                                                   !3, ~15
    9    17      > JMPZ_EX                                          ~17     !3, ->21
         18    >   STRLEN                                           ~18     !2
         19        IS_EQUAL                                         ~19     ~18, 2
         20        BOOL                                             ~17     ~19
         21    > > JMPZ                                                     ~17, ->77
   11    22    >   INIT_FCALL                                               'str_replace'
         23        SEND_VAL                                                 'mm'
         24        SEND_VAL                                                 '%280%3F%5B1-9%5D%7C1%5B0-2%5D%29'
         25        SEND_VAR                                                 !1
         26        DO_ICALL                                         $20     
         27        ASSIGN                                                   !4, $20
   12    28        INIT_FCALL                                               'str_replace'
         29        SEND_VAL                                                 'dd'
         30        SEND_VAL                                                 '%280%3F%5B1-9%5D%7C%5B1-2%5D%5B0-9%5D%7C3%5B0-1%5D%29'
         31        SEND_VAR                                                 !4
         32        DO_ICALL                                         $22     
         33        ASSIGN                                                   !4, $22
   13    34        INIT_FCALL                                               'str_replace'
         35        SEND_VAL                                                 'yyyy'
         36        SEND_VAL                                                 '%2819%7C20%29%3F%5B0-9%5D%5B0-9%5D'
         37        SEND_VAR                                                 !4
         38        DO_ICALL                                         $24     
         39        ASSIGN                                                   !4, $24
   14    40        INIT_FCALL                                               'str_replace'
         41        SEND_VAR                                                 !3
         42        CONCAT                                           ~26     '%5C', !3
         43        SEND_VAL                                                 ~26
         44        SEND_VAR                                                 !4
         45        DO_ICALL                                         $27     
         46        ASSIGN                                                   !4, $27
   15    47        IS_NOT_EQUAL                                     ~29     !4, !0
         48      > JMPZ_EX                                          ~29     ~29, ->56
         49    >   INIT_FCALL                                               'preg_match'
         50        CONCAT                                           ~30     '%2F', !4
         51        CONCAT                                           ~31     ~30, '%5Cz%2F'
         52        SEND_VAL                                                 ~31
         53        SEND_VAR                                                 !0
         54        DO_ICALL                                         $32     
         55        BOOL                                             ~29     $32
         56    > > JMPZ                                                     ~29, ->77
   18    57    >   INIT_FCALL                                               'explode'
         58        SEND_VAR                                                 !3
         59        SEND_VAR                                                 !0
         60        DO_ICALL                                         $33     
         61        ASSIGN                                                   !5, $33
   19    62        FETCH_DIM_R                                      ~35     !5, 0
         63        ASSIGN                                                   !6, ~35
   20    64        FETCH_DIM_R                                      ~37     !5, 1
         65        ASSIGN                                                   !7, ~37
   21    66        FETCH_DIM_R                                      ~39     !5, 2
         67        ASSIGN                                                   !8, ~39
   22    68        BEGIN_SILENCE                                    ~41     
         69        INIT_FCALL                                               'checkdate'
         70        SEND_VAR                                                 !7
         71        SEND_VAR                                                 !6
         72        SEND_VAR                                                 !8
         73        DO_ICALL                                         $42     
         74        END_SILENCE                                              ~41
         75      > JMPZ                                                     $42, ->77
   23    76    > > RETURN                                                   <true>
   27    77    > > RETURN                                                   <false>
   28    78*     > RETURN                                                   null

End of function is_valid_date

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.85 ms | 1404 KiB | 23 Q