Monday, November 17, 2008

Convert Date into SQL Date Format

In this function you can easily convert user input date format into SQL date format.
this 00/00/0000 to 0000-00-00

Example:
$date = "15/11/2008";
$query = "INSERT INTO TB VALUES('".tosqldate($date)."')";
// result: 2008-11-15


function tosqldate($strdata){
$strdata = trim($strdata);
if($strdata==""){
return "";
}
$arrdate = explode("/",$strdata);
if(count($arrdate) < 3){
return "";
}else{
return $arrdate[2]."-".$arrdate[1]."-".$arrdate[0];
}
}
?>

No comments: