1

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!

Charles
  • 50,943
  • 13
  • 104
  • 142
Ryan
  • 17,511
  • 23
  • 63
  • 88

1 Answers1

3

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.

Darryl E. Clarke
  • 7,537
  • 3
  • 26
  • 34
  • Unfortunately, Zend_Validate_Date also uses Zend_Date, so it has the same problems. See http://framework.zend.com/issues/browse/ZF-7583 – JW. Oct 08 '13 at 15:57