13.4. Working with Dates and Times

Zend_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.

13.4.1. Normalizing Dates and Times

getDate() 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. 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 supressed 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.

The following return values are possible, when the BCMath extension is available:

表 13.2. Return values

Array key Returned value Minimum Maximum
day integer 1 31
month integer 1 12
year integer no limit no limit
hour integer 0 no limit
minute integer 0 59
second integer 0 59

例 13.28. Normalizing a date

<?php
require_once 'Zend/Locale.php';

$date = Zend_Locale_Format::getDate('13.04.2006', 'dd.MM.yyyy');

print_r ($date);
?>

Since getDate() is "locale-aware", specifying the $locale is sufficient for date strings adhering to that locale's format.

例 13.29. Normalizing a date by locale

<?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('de_AT');
$date = Zend_Locale_Format::getDate('13.04.2006', null, $locale);

print_r ($date);
?>

A complete date and time is returned when the input contains both a date and time in the expected format.

例 13.30. Normalizing a date with time

<?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('de_AT');
$date = Zend_Locale_Format::getDate('13.04.2005 22:14:55', false, $locale);

print_r ($date);
?>

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.

例 13.31. Normalizing a userdefined date

<?php
require_once 'Zend/Locale.php';

$date = Zend_Locale_Format::getDate('13200504T551422', 'ddyyyyMM ssmmHH');

print_r ($date);
?>

The format can include the following signs :

表 13.3. Format definition

Format Letter Description
d or dd 1 or 2 digit day
M or MM 1 or 2 digit month
y or yy 1 or 2 digit year
yyyy 4 digit year
h 1 or 2 digit hour
m 1 or 2 digit minute
s 1 or 2 digit second

Examples for proper formats are

表 13.4. Example formats

Formats Input Output
dd.MM.yy 1.4.6 ['day'] => 1, ['month'] => 4, ['year'] => 6
dd.MM.yy 01.04.2006 ['day'] => 1, ['month'] => 4, ['year'] => 2006
yyyyMMdd 1.4.6 ['day'] => 6, ['month'] => 4, ['year'] => 1

[注意] Database date format

To parse a database date value (f.e. MySql or MsSql), use Zend_Date's ISO_8601 format instead of getDate().

getDate() automatically detects and corrects some kinds of problems with input, such as misplacing the year:

例 13.32. Correction for date normalizing

<?php
require_once 'Zend/Locale.php';

$date = Zend_Locale_Format::getDate('41.10.20', 'ddMMyy');

// instead of 41 for the day, the 41 will be returned as year value
print_r ($date);
?>

13.4.2. Testing Dates

Use isDate() to check if a given string contains a valid date.

例 13.33. Date testing

<?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('de_AT');
if (Zend_Locale_Format::isDate('13.Apr.2006', $locale) {
    print "date";
} else {
    print "not a date";
} 
?>

13.4.3. Normalizing a Time

Normally, 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.

例 13.34. Normalize an unknown time

<?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('de_AT');
if (Zend_Locale_Format::getTime('13:44:42', $locale) {
    print "time";
} else {
    print "not a time";
} 
?>

13.4.4. Testing Times

Use isTime() to check if a given string contains a proper time.

例 13.35. Testing a time

<?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('de_AT');
if (Zend_Locale_Format::isTime('13:44:42', $locale) {
    print "time";
} else {
    print "not a time";
} 
?>