Friday
20
February
2009

Validation and formatting of Dates

Hy fellows,

when you are validating Input with Zend_Validate_Date then you can now use the Date constants as validation format. This allows you to validate against localized formats even when you don’t know how the format looks like.

As example:
In past you had to know the format or evaluate it from Zend_Locale like shown:

$locale = 'de';
$format = Zend_Locale::getTranslation('date', array('gregorian', 'medium'), $locale);
$valid = new Zend_Validate_Date($format, $locale);

Now you can simply use the constants from Zend_Date to simplify the validation:

$valid = new Zend_Validate_Date(Zend_Date::DATE_MEDIUM, 'de');

This works with all available date constants.

Also Zend_Date itself can now handle the format constants within the methods which did not support it. You can use them within isDate() which, until now, only accepted fixed ISO format. And you can use them also within the toString() method.

$date = new Zend_Date($myinput);
print $date->toString(Zend_Date::RSS);

While adding this new features I also did some rework of Zend_Date itself. It is now about 200 lines reduced without change to the functionallity. And the parsing is faster than in past.

As always, this new feature is available with the next minor release which will be 1.8.

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top