To check a date is entered in YYYY-MM-DD format.
<?php
if(preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $datein)){
//it's ok
}else{
//it's bad
}
// another way
$datein = '1998-09-21';
if(preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $datein)){
echo 'go';
}else{
echo 'no go';
}
//with function
function MyCheckDate( $postedDate ) {
if ( ereg("^[0-9]{4}-[01][0-9]-[0-3][0-9]$",$postedDate) ) {
list( $year , $month , $day ) = explode('-',$postedDate);
return( checkdate( $month , $day , $year ) );
} else {
return( false );
}
}
function MyCheckDate( $postedDate ) {
if ( preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $postedDate) ) {
list($year , $month , $day) = explode('-',$postedDate);
return checkdate($month , $day , $year);
} else {
return false;
}
}
//
function MyCheckDate( $postedDate ) {
if ( preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $postedDate) ) {
list($year , $month , $day) = explode('-',$postedDate);
return checkdate($month , $day , $year);
} else {
return false;
}
//
function MyCheckDate( $postedDate ) {
if (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $postedDate, $datebit)) {
return checkdate($datebit[2] , $datebit[3] , $datebit[1]);
} else {
return false;
}
}
?>
<?php
if(preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $datein)){
//it's ok
}else{
//it's bad
}
// another way
$datein = '1998-09-21';
if(preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $datein)){
echo 'go';
}else{
echo 'no go';
}
//with function
function MyCheckDate( $postedDate ) {
if ( ereg("^[0-9]{4}-[01][0-9]-[0-3][0-9]$",$postedDate) ) {
list( $year , $month , $day ) = explode('-',$postedDate);
return( checkdate( $month , $day , $year ) );
} else {
return( false );
}
}
function MyCheckDate( $postedDate ) {
if ( preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $postedDate) ) {
list($year , $month , $day) = explode('-',$postedDate);
return checkdate($month , $day , $year);
} else {
return false;
}
}
//
function MyCheckDate( $postedDate ) {
if ( preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $postedDate) ) {
list($year , $month , $day) = explode('-',$postedDate);
return checkdate($month , $day , $year);
} else {
return false;
}
//
function MyCheckDate( $postedDate ) {
if (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $postedDate, $datebit)) {
return checkdate($datebit[2] , $datebit[3] , $datebit[1]);
} else {
return false;
}
}
?>
0 comments:
Post a Comment