3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getFormattedDate(string $date, string $format = 'm/d/Y', string $yearPadding = '19'): string { $sanitizedDate = 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, $sanitizedDate); // attempt to parse if (!$d) { throw new Exception("Could not parse date: $date"); } $formattedDate = $d->format($format); if ($formattedDate !== $sanitizedDate) { throw new Exception("Date not in a desired American format: $date"); } else { return $date; } } $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' ]; foreach ($tests as $test) { try { echo getFormattedDate($test); } catch (Exception $e) { echo $e->getMessage(); } echo "\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 14
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 14
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
Found catch point at position: 8
Branch analysis from position: 8
2 jumps found. (Code = 107) Position 1 = 9, Position 2 = -2
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
filename:       /in/U9FME
function name:  (null)
number of ops:  16
compiled vars:  !0 = $tests, !1 = $test, !2 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   ASSIGN                                                   !0, <array>
   40     1      > FE_RESET_R                                       $4      !0, ->14
          2    > > FE_FETCH_R                                               $4, !1, ->14
   42     3    >   INIT_FCALL                                               'getformatteddate'
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $5      
          6        ECHO                                                     $5
          7      > JMP                                                      ->12
   43     8  E > > CATCH                                       last         'Exception'
   44     9    >   INIT_METHOD_CALL                                         !2, 'getMessage'
         10        DO_FCALL                                      0  $6      
         11        ECHO                                                     $6
   46    12    >   ECHO                                                     '%0A'
   40    13      > JMP                                                      ->2
         14    >   FE_FREE                                                  $4
   47    15      > RETURN                                                   1

Function getformatteddate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 24
Branch analysis from position: 18
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 37
Branch analysis from position: 30
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/U9FME
function name:  getFormattedDate
number of ops:  41
compiled vars:  !0 = $date, !1 = $format, !2 = $yearPadding, !3 = $sanitizedDate, !4 = $d, !5 = $formattedDate
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                                  '%00%7Bclosure%7D%2Fin%2FU9FME%3A6%240'
          6        BIND_LEXICAL                                             ~6, !2
          7        SEND_VAL                                                 ~6
    7     8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $7      
    4    10        ASSIGN                                                   !3, $7
    9    11        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         12        SEND_VAR                                                 !1
         13        SEND_VAR                                                 !3
         14        DO_FCALL                                      0  $9      
         15        ASSIGN                                                   !4, $9
   10    16        BOOL_NOT                                         ~11     !4
         17      > JMPZ                                                     ~11, ->24
   11    18    >   NEW                                              $12     'Exception'
         19        NOP                                                      
         20        FAST_CONCAT                                      ~13     'Could+not+parse+date%3A+', !0
         21        SEND_VAL_EX                                              ~13
         22        DO_FCALL                                      0          
         23      > THROW                                         0          $12
   13    24    >   INIT_METHOD_CALL                                         !4, 'format'
         25        SEND_VAR_EX                                              !1
         26        DO_FCALL                                      0  $15     
         27        ASSIGN                                                   !5, $15
   14    28        IS_NOT_IDENTICAL                                         !5, !3
         29      > JMPZ                                                     ~17, ->37
   15    30    >   NEW                                              $18     'Exception'
         31        NOP                                                      
         32        FAST_CONCAT                                      ~19     'Date+not+in+a+desired+American+format%3A+', !0
         33        SEND_VAL_EX                                              ~19
         34        DO_FCALL                                      0          
         35      > THROW                                         0          $18
         36*       JMP                                                      ->39
   17    37    >   VERIFY_RETURN_TYPE                                       !0
         38      > RETURN                                                   !0
   19    39*       VERIFY_RETURN_TYPE                                       
         40*     > RETURN                                                   null

End of function getformatteddate

Function %00%7Bclosure%7D%2Fin%2FU9FME%3A6%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/U9FME
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 function %00%7Bclosure%7D%2Fin%2FU9FME%3A6%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
168.97 ms | 1407 KiB | 20 Q