简体   繁体   中英

Zend_Date::isDate won't enforce $format

I've having an issue where Zend_Date::isDate is returning true even when the value doesn't necessarily match the format given.

For example:

$time = "12:34:56"; // Time doesn't have AM/PM attached

if( Zend_Date::isDate($time, 'hh:mm:ss a') )
    echo "this is true";
else
    echo "this is false";

This is always true, even though the format lists the 'a' indicating it needs "Time of day, localized" (which in my case is "AM"/"PM"). Even a completely mis-formatted time, like '12:12:34:56:56' will still return true. A time of ':34:56' will return false however. Is this a bug or am I missing something in thinking the format I give it is what needs to be matched?

Thanks!

I don't think Zend_Date is built to 'enforce' a format. It's built to answer "If I give Zend_Date this string, can you shove it in this format?" It's really misleading.

In your example, it's true, but it actually evaluates to:

Jan 25, 35 12:00:00 AM

Which you probably didn't expect.

In your bogus example, it's also true, it evaluates to:

Dec 14, 34 8:56:00 AM (Dec 12, 34 + 56 hours and 56 minutes)

To validate a date, which I think you want to do is use a Zend_Validator...

    $validator = new Zend_Validate_Date(array('format' => 'hh:mm:ss a'));
    var_dump($validator->isValid("12:34:56 pm")); // true
    var_dump($validator->isValid("14:34:56 pm")); // false, actually 2pm
    var_dump($validator->isValid("01:11:11 am")); // true
    var_dump($validator->isValid("01:11:11 xm")); // true, 24h fallback
    var_dump($validator->isValid("24:01:01")); // false
    var_dump($validator->isValid("16:01:01")); // true, 24h fallback

If you run your dates through validation first, then into Zend_Date you should get expected results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM