Working with Dates and TimesZend_Locale_Format provides several methods for working with dates and times to help convert and normalize between different formats for different locales. Use Zend_Date for manipulating dates, and working with date strings that already conform to one of the many internationally recognized standard formats, or one of the localized date formats supported by Zend_Date. Using an existing, pre-defined format offers advantages, including the use of well-tested code, and the assurance of some degree of portability and interoperability (depending on the standard used). The examples below do not follow these recommendations, since using non-standard date formats would needlessly increase the difficulty of understanding these examples. Normalizing Dates and TimesThe getDate() method parses strings containing dates in localized formats. The results are returned in a structured array, with well-defined keys for each part of the date. In addition, the array will contain a key 'date_format' showing the format string used to parse the input date string. Since a localized date string may not contain all parts of a date/time, the key-value pairs are optional. for example, if only the year, month, and day is given, then all time values are suppressed from the returned array, and vice-versa if only hour, minute, and second were given as input. If no date or time can be found within the given input, an exception will be thrown. If setOption(array('fix_date' => true)) is set the getDate() method adds a key 'fixed' with a whole number value indicating if the input date string required "fixing" by rearranging the day, month, or year in the input to fit the format used.
For those needing to specify explicitly the format of the date string, the following format token specifiers are supported. If an invalid format specifier is used, such as the PHP 'i' specifier when in ISO format mode, then an error will be thrown by the methods in Zend_Locale_Format that support user-defined formats. These specifiers (below) are a small subset of the full "ISO" set supported by Zend_Date's toString(). If you need to use PHP date() compatible format specifiers, then first call setOptions(array('format_type' => 'php')). And if you want to convert only one special format string from PHP date() compatible format to "ISO" format use convertPhpToIsoFormat(). Currently, the only practical difference relates to the specifier for minutes ('m' using the ISO default, and 'i' using the PHP date format).
Example #1 Normalizing a date
Since getDate() is "locale-aware", specifying the
$locale is sufficient for date strings adhering to that locale's
format. The option ' Example #2 Normalizing a date by locale A complete date and time is returned when the input contains both a date and time in the expected format. Example #3 Normalizing a date with time If a specific format is desired, specify the $format argument, without giving a $locale. Only single-letter codes (H, m, s, y, M, d), and MMMM and EEEE are supported in the $format. Example #4 Normalizing a userdefined date The format can include the following signs :
Examples for proper formats are
The option ' Testing Dates
Use checkDateFormat($inputString, array('date_format' => $format,
$locale)) to check if a given string contains all expected date parts.
The checkDateFormat() method uses
getDate(), but without the option Normalizing a TimeNormally, a time will be returned with a date, if the input contains both. If the proper format is not known, but the locale relevant to the user input is known, then getTime() should be used, because it uses the default time format for the selected locale. Testing Times
Use checkDateFormat() to check if a given string contains a
proper time. The usage is exact the same as with checking Dates, only
|