Parse and Format Dates between PHP and Mysql
Mysql Database stores dates in the format :
yyyy-mm-dd e.g. 2009-04-15
Whereas humans are more used to the formats like :
dd-mm-yyyy or mm-dd-yyyy e.g. 21-04-2009
And while reading a format like :
dd-Month-yyyy is more usable e.g. 21-Mar-2009
Checking a valid date :
If the date is in format dd-mm-yyyy then a simple way to test its validity would be :
if(strtotime(implode('-', array_reverse(explode('-', $date)))))
return true;
But the above has an issue that it will report 31-02-2009 as a valid date. The same with strptime.
Another function checkdate is useful for this issue and it also manages leap years.
$datearr = explode('-', $date);
list($d,$m,$y) = $datearr;
if(checkdate($m, $d, $y))
return true;
But a small issue with checkdate is that it reports a date like 01-01-2500 to be valid again (which is , as per what checkdate is supposed to do).
Using strptime to check a date being in a valid dd-mm-yyyy format :
/*
Check date to be in format dd-mm-yyyy
*/
function valid_date($date) {
#Check format
if(!strptime($date , '%d-%m-%Y'))
return false;
$parts = explode('-' , $date);
$d = $parts[0];
$m = $parts[1];
$y = $parts[2];
#Check format of year number
if(!($y >= 1000 && $y <= 9999))
return false;
return true;
}
or more simply :
if(strtotime(implode('-', array_reverse(explode('-', $date)))))
return true;
else
return false;
Using strptime to convert a date in dd-mm-yyyy format to yyyy-mm-dd the code could be :
/*
Convert a dd-mm-yyyy into yyyy-mm-dd for mysql insertion
*/
function dbdate($date) {
$d = strptime($date , '%d-%m-%Y'); #This is the user input format
if($d)
$date = ($d['tm_year']+1900).'-'.($d['tm_mon']+1).'-'.$d['tm_mday'];
else
$date = '1800-01-01';
return $date;
}
or more simply :
if(strtotime(implode('-', array_reverse(explode('-', $date)))))
return implode('-', array_reverse(explode('-', $date)));
else
return '1800-01-01';
The above function would convert a date in the format dd-mm-yyyy to yyyy-mm-dd.
e.g. $date = dbdate($date) where $date = “02-04-2006″
now $date is ready to be inserted in the mysql database
Checking date format and range :
function valid_date($date , $range=0) {
$datearr = explode('-', $date);
if(count($datearr) != 3)
return false;
list($d, $m, $y) = $datearr;
#Check validity
if($range == 1) {
#Check date format with range and validity
if(checkdate($m , $d , $y) and strtotime("$y-$m-$d")) #strtotime to invalidate outrange dates like 01-01-2500
return true;
}
else {
#Check date format and validity and not range
if(checkdate($m , $d , $y)) #strtotime to invalidate outrange dates like 01-01-2500
return true;
}
return false;
}
The above function can just check the date format or the range as well depending on the value of the $range parameter. checkdate() invalidates dates like 31-02-2009 and takes care of leap years as well. Whereas strotime checks the range of the date. The above function checks the format dd-mm-yyyy and dd-mm-yy. A little modification would adapt it to formats dd-mm-yyyy , dd-mm-yy dd/mm/yyyy and dd/mm/yy :
/*
Checks the date validity in formats :
1. dd-mm-yyyy
2. dd-mm-yy
3. dd/mm/yyyy
4. dd/mm/yy
*/
function valid_date($date , $range=0) {
#For valid dd/mm/yyyy
$date = str_replace('/', '-', $date);
#Check
$datearr = explode('-', $date);
if(count($datearr) != 3)
return false;
list($d, $m, $y) = $datearr;
#Check validity
if($range == 1) {
#Check date format with range and validity
if(checkdate($m , $d , $y) and strtotime("$y-$m-$d")) #strtotime to invalidate outrange dates like 01-01-2500
return true;
}
else {
#Check date format and validity and not range
if(checkdate($m , $d , $y)) #strtotime to invalidate outrange dates like 01-01-2500
return true;
}
return false;
}
/*
Give a dd-month-yyyy representation of mysql format yyyy-mm-dd
*/
function showdate($dbdate) {
$stamp = strtotime($dbdate);
if($stamp)
return strftime("%d-%b-%Y",$stamp); #This is the user viewing format
else
return '';
}
While printing dates in a table for viewing purpose a format like 02-Mar-2010 is easily readable and the above function does the same. It converts 2010-03-02 to 02-Mar-2010.
/*
Convert yyyy-mm-dd to dd-mm-yyyy
*/
function feeddate($dbdate) {
$stamp = strtotime($dbdate);
if($stamp)
return strftime("%d-%m-%Y",$stamp); #This is the user viewing format
else
return '';
}
Popularity: 4% [?]















