PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Checking Dates

Probably most dates you will get as input are strings. But the problem with strings is that you can not be sure if the string is a real date. Therefor Zend_Date has spent an own static function to check date strings. Zend_Locale has an own function getDate($date, $locale) which parses a date and returns the proper and normalized date parts. A monthname for example will be recognised and returned just a month number. But as Zend_Locale does not know anything about dates because it is a normalizing and localizing class we have integrated an own function isDate($date) which checks this.

isDate($date, $format, $locale) can take up to 3 parameters and needs minimum one parameter. So what we need to verify a date is, of course, the date itself as string. The second parameter can be the format which the date is expected to have. If no format is given the standard date format from your locale is used. For details about how formats should look like see the chapter about self defined formats.

The third parameter is also optional as the second parameter and can be used to give a locale. We need the locale to normalize monthnames and daynames. So with the third parameter we are able to recognise dates like '01.Jänner.2000' or '01.January.2000' depending on the given locale.

isDate() of course checks if a date does exist. Zend_Date itself does not check a date. So it is possible to create a date like '31.February.2000' with Zend_Date because Zend_Date will automatically correct the date and return the proper date. In our case '03.March.2000'. isDate() on the other side does this check and will return FALSE on '31.February.2000' because it knows that this date is impossible.

Example 180. Checking Dates

<?php
// Checking dates
$date '01.03.2000';
if (
Zend_Date::isDate($date)) {
    print 
"String $date is a date";
} else {
    print 
"String $date is NO date";
}

// Checking localized dates
$date '01 February 2000';
if (
Zend_Date::isDate($date,'dd MMMM yyyy''en')) {
    print 
"String $date is a date";
} else {
    print 
"String $date is NO date";
}

// Checking impossible dates
$date '30 February 2000';
if (
Zend_Date::isDate($date,'dd MMMM yyyy''en')) {
    print 
"String $date is a date";
} else {
    print 
"String $date is NO date";
}

Zend Framework