Reputation:
In Zend Framework 1.x there is Zend_Date() to validate (German Date and Time)
/* input $df-dtDauer for example "16:59" */
if(!Zend_Date::isDate($df_dtDauer_bis,'HH:mm')) {
$this->_aMessage[] = 'ungültige "Endezeit": '.$df_dtDauer_bis;
$bRet = false;
}
How to do this with native PHP (5.3.x)?
Upvotes: 0
Views: 157
Reputation: 4635
You can also use the DateTime as suggested by mario.
A function like this will help you to validate the format.
function validateDate($date, $format = 'Y-m-d H:i:s')
{
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}
var_dump(validateDate('14:50', 'H:i')); # true
var_dump(validateDate('14:77', 'H:i')); # false
var_dump(validateDate(14, 'H')); # true
var_dump(validateDate('14', 'H')); # true
function was copied from this answer or php.net
Upvotes: 2