Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
php regex for date (id)
<?php
echo '<h2>yyyy/mm/dd</h2>';
preg_match('/(19[5-9][0-9]|20[0-9][0-9])[\/-](0[1-9]|1[0-2])[\/-](0[1-9]|1[0-9]|2[0-9]|3[01])/','2011/12/30',$match);
foreach($match as $key=>$val){
echo $key . ' => ' . $val . '<br>';
}
echo '<h2>dd/mm/yyyy</h2>';
preg_match('/(0[1-9]|1[0-9]|2[0-9]|3[01])[\/-](0[1-9]|1[0-2])[\/-](19[5-9][0-9]|20[0-9][0-9])/','30/12/2011',$match);
foreach($match as $key=>$val){
echo $key . ' => ' . $val . '<br>';
}
echo '<h2>mm/dd/yyyy</h2>';
preg_match('/(0[1-9]|1[0-2])[\/-](0[1-9]|1[0-9]|2[0-9]|3[01])[\/-](19[5-9][0-9]|20[0-9][0-9])/','12/30/2011',$match);
foreach($match as $key=>$val){
echo $key . ' => ' . $val . '<br>';
}
echo '<h2>yyyy-mm-dd</h2>';
preg_match('/(19[5-9][0-9]|20[0-9][0-9])[\/-](0[1-9]|1[0-2])[\/-](0[1-9]|1[0-9]|2[0-9]|3[01])/','2011-12-30',$match);
foreach($match as $key=>$val){
echo $key . ' => ' . $val . '<br>';
}
echo '<h2>dd-mm-yyyy</h2>';
preg_match('/(0[1-9]|1[0-9]|2[0-9]|3[01])[\/-](0[1-9]|1[0-2])[\/-](19[5-9][0-9]|20[0-9][0-9])/','30-12-2011',$match);
foreach($match as $key=>$val){
echo $key . ' => ' . $val . '<br>';
}
echo '<h2>mm-dd-yyyy</h2>';
preg_match('/(0[1-9]|1[0-2])[\/-](0[1-9]|1[0-9]|2[0-9]|3[01])[\/-](19[5-9][0-9]|20[0-9][0-9])/','12-30-2011',$match);
foreach($match as $key=>$val){
echo $key . ' => ' . $val . '<br>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment