Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want it to be exactly like PHP 5.3 function, you're gonna need a lot of code. I'd start with something like this:</p> <pre><code>$format = '\Y: Y-m-d'; var_dump($format); $date = date($format); var_dump($date); // reverse engineer date formats $keys = array( 'Y' =&gt; array('year', '\d{4}'), 'm' =&gt; array('month', '\d{2}'), 'd' =&gt; array('day', '\d{2}'), 'j' =&gt; array('day', '\d{1,2}'), 'n' =&gt; array('month', '\d{1,2}'), 'M' =&gt; array('month', '[A-Z][a-z]{2}'), 'F' =&gt; array('month', '[A-Z][a-z]{2,8}'), 'D' =&gt; array('day', '[A-Z][a-z]{2}'), // etc etc etc ); // convert format string to regex $regex = ''; $chars = str_split($format); foreach ( $chars AS $n =&gt; $char ) { $lastChar = isset($chars[$n-1]) ? $chars[$n-1] : ''; $skipCurrent = '\\' == $lastChar; if ( !$skipCurrent &amp;&amp; isset($keys[$char]) ) { $regex .= '(?P&lt;'.$keys[$char][0].'&gt;'.$keys[$char][1].')'; } else if ( '\\' == $char ) { $regex .= $char; } else { $regex .= preg_quote($char); } } var_dump($regex); // now try to match it if ( preg_match('#^'.$regex.'$#', $date, $matches) ) { foreach ( $matches AS $k =&gt; $v ) if ( is_int($k) ) unset($matches[$k]); print_r($matches); } else { echo 'invalid date "'.$date.'" for format "'.$format.'"'."\n"; } </code></pre> <p>Result:</p> <pre><code>string(9) "\Y: Y-m-d" string(13) "Y: 2011-07-12" string(51) "\Y\: (?P&lt;year&gt;\d{4})-(?P&lt;month&gt;\d{2})-(?P&lt;day&gt;\d{2})" Array ( [year] =&gt; 2011 [month] =&gt; 07 [day] =&gt; 12 ) </code></pre> <p>Incomplete and imperfect.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload