user4420255
user4420255

Reputation:

Check valid time without zend_date (but with native php)

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

Answers (1)

Nandakumar V
Nandakumar V

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

Related Questions