9.6. Constants for General Date Functions

Whenever a Zend_Date method has a $parts parameter, one of the constants below can be used as the argument for that parameter, in order to select a specific part of a date or indicate the date format used or desired (e.g. RFC 822).

9.6.1. Using Constants

For example, the constant Zend_Date::HOUR can be used in the ways shown below. When working with days of the week, calendar dates, hours, minutes, seconds, and any other date parts that are expressed differently when in different parts of the world, the object's timezone will automatically be used to compute the correct value, even though the internal timestamp is the same for the same moment in time, regardless of the user's physical location in the world. Regardless of the units involved, output must be expressed either as GMT/UTC or localized to a locale. The example output below reflects localization to Europe/GMT+1 hour (e.g. Germany, Austria, France).

Таблица 9.7. Operations involving Zend_Date::HOUR

Function/input Description Original date Effect/output
get(Zend_Date::HOUR) Output of the hour 2009-02-13T14:53:27+01:00 14
set(12, Zend_Date::HOUR) Set new hour 2009-02-13T14:53:27+01:00 2009-02-13T12:53:27+01:00
add(12, Zend_Date::HOUR) Add hours 2009-02-13T14:53:27+01:00 2009-02-14T02:53:27+01:00
sub(12, Zend_Date::HOUR) Subtract hours 2009-02-13T14:53:27+01:00 2009-02-13T02:53:27+01:00
compare(12, Zend_Date::HOUR) Compare hour, returns 0, 1 or -1 2009-02-13T14:53:27+01:00 1 (if object > argument)
copy(Zend_Date::HOUR) Copies only the hour part 2009-02-13T14:53:27+01:00 1970-01-01T14:00:00+01:00
equals(14, Zend_Date::HOUR) Compares the hour, returns TRUE or FALSE 2009-02-13T14:53:27+01:00 TRUE
isEarlier(12, Zend_Date::HOUR) Compares the hour, returns TRUE or FALSE 2009-02-13T14:53:27+01:00 TRUE
isLater(12, Zend_Date::HOUR) Compares the hour, returns TRUE or FALSE 2009-02-13T14:53:27+01:00 FALSE

9.6.2. List of All Constants

Each part of a date/time has a unique constant in Zend_Date. All constants supported by Zend_Date are listed below.

Таблица 9.8. Day Constants

Constant Description Date Affected part/example
Zend_Date::DAY Day (as a number, two digit) 2009-02-06T14:53:27+01:00 2009-02-06T14:53:27+01:00 (06)
Zend_Date::DAY_SHORT Day (as a number, one or two digit) 2009-02-06T14:53:27+01:00 2009-02-06T14:53:27+01:00 (6)
Zend_Date::WEEKDAY Weekday (Name of the day, localized, complete) 2009-02-06T14:53:27+01:00 Friday
Zend_Date::WEEKDAY_SHORT Weekday (Name of the day, localized, abbreviated, the first three digits) 2009-02-06T14:53:27+01:00 Fre for Friday
Zend_Date::WEEKDAY_NAME Weekday (Name of the day, localized, abbreviated, the first two digits) 2009-02-06T14:53:27+01:00 Fr for Friday
Zend_Date::WEEKDAY_NARROW Weekday (Name of the day, localized, abbreviated, only the first digit) 2009-02-06T14:53:27+01:00 F for Friday
Zend_Date::WEEKDAY_DIGIT Weekday (0 = Sunday, 6 = Saturday) 2009-02-06T14:53:27+01:00 5 for Friday
Zend_Date::WEEKDAY_8601 Weekday according to ISO 8601 (1 = Monday, 7 = Sunday) 2009-02-06T14:53:27+01:00 5 for Friday
Zend_Date::DAY_OF_YEAR Day (as a number, one or two digit) 2009-02-06T14:53:27+01:00 43
Zend_Date::DAY_SUFFIX English addendum for the day (st, nd, rd, th) 2009-02-06T14:53:27+01:00 th

Таблица 9.9. Week Constants

Constant Description Date Affected part/example
Zend_Date::WEEK Week (as a number, 1-53) 2009-02-06T14:53:27+01:00 7

Таблица 9.10. Month Constants

Constant Description Date Affected part/example
Zend_Date::MONTH_NAME Month (Name of the month, localized, complete) 2009-02-06T14:53:27+01:00 February
Zend_Date::MONTH_NAME_SHORT Month (Name of the month, localized, abbreviated, three digit) 2009-02-06T14:53:27+01:00 Feb
Zend_Date::MONTH_NAME_NARROW Month (Name of the month, localized, abbreviated, one digit) 2009-02-06T14:53:27+01:00 F
Zend_Date::MONTH Month (Number of the month, two digit) 2009-02-06T14:53:27+01:00 2009-02-06T14:53:27+01:00 (02)
Zend_Date::MONTH_SHORT Month (Number of the month, one or two digit) 2009-02-06T14:53:27+01:00 2009-02-06T14:53:27+01:00 (2)
Zend_Date::MONTH_DAYS Number of days for this month (number) 2009-02-06T14:53:27+01:00 28

Таблица 9.11. Year Constants

Constant Description Date Affected part/example
Zend_Date::YEAR Year (number) 2009-02-06T14:53:27+01:00 2009-02-06T14:53:27+01:00
Zend_Date::YEAR_8601 Year according to ISO 8601 (number) 2009-02-06T14:53:27+01:00 2009
Zend_Date::YEAR_SHORT Year (number, two digit) 2009-02-06T14:53:27+01:00 2009-02-06T14:53:27+01:00
Zend_Date::YEAR_SHORT_8601 Year according to ISO 8601 (number, two digit) 2009-02-06T14:53:27+01:00 09
Zend_Date::LEAPYEAR Is the year a leap year? (TRUE or FALSE) 2009-02-06T14:53:27+01:00 FALSE

Таблица 9.12. Time Constants

Constant Description Date Affected part/example
Zend_Date::HOUR Hour (00-23, two digit) 2009-02-06T14:53:27+01:00 14
Zend_Date::HOUR_SHORT Hour (0-23, one or two digit) 2009-02-06T14:53:27+01:00 14
Zend_Date::HOUR_SHORT_AM Hour (1-12, one or two digit) 2009-02-06T14:53:27+01:00 2
Zend_Date::HOUR_AM Hour (01-12, two digit) 2009-02-06T14:53:27+01:00 02
Zend_Date::MINUTE Minute (00-59, two digit) 2009-02-06T14:53:27+01:00 2009-02-06T14:53:27+01:00
Zend_Date::MINUTE_SHORT Minute (0-59, one or two digit) 2009-02-06T14:03:27+01:00 2009-02-06T14:03:27+01:00
Zend_Date::SECOND Second (00-59, two digit) 2009-02-06T14:53:27+01:00 2009-02-06T14:53:27+01:00
Zend_Date::SECOND_SHORT Second (0-59, one or two digit) 2009-02-06T14:53:07+01:00 2009-02-06T14:53:07+01:00
Zend_Date::MILLISECOND Millisecond (theoretically infinite) 2009-02-06T14:53:27.20546 2009-02-06T14:53:27.20546
Zend_Date::MERIDIEM Time of day (forenoon/afternoon) 2009-02-06T14:53:27+01:00 afternoon
Zend_Date::SWATCH Swatch Internet Time 2009-02-06T14:53:27+01:00 620

Таблица 9.13. Timezone Constants

Constant Description Date Affected part/example
Zend_Date::TIMEZONE Name der time zone (string, abbreviated) 2009-02-06T14:53:27+01:00 CET
Zend_Date::TIMEZONE_NAME Name of the time zone (string, complete) 2009-02-06T14:53:27+01:00 Europe/Paris
Zend_Date::TIMEZONE_SECS Difference of the time zone to GMT in seconds (integer) 2009-02-06T14:53:27+01:00 3600 seconds to GMT
Zend_Date::GMT_DIFF Difference to GMT in seconds (string) 2009-02-06T14:53:27+01:00 +0100
Zend_Date::GMT_DIFF_SEP Difference to GMT in seconds (string, separated) 2009-02-06T14:53:27+01:00 +01:00
Zend_Date::DAYLIGHT Summer time or Winter time? (TRUE or FALSE) 2009-02-06T14:53:27+01:00 FALSE

Таблица 9.14. Date Format Constants (formats include timezone)

Constant Description Date Affected part/example
Zend_Date::ISO_8601 Date according to ISO 8601 (string, complete) 2009-02-13T14:53:27+01:00 2009-02-13T14:53:27+01:00
Zend_Date::RFC_2822 Date according to RFC 2822 (string) 2009-02-13T14:53:27+01:00 Fri, 13 Feb 2009 14:53:27 +0100
Zend_Date::TIMESTAMP Unix time (seconds since 1.1.1970, mixed) 2009-02-13T14:53:27+01:00 1234533207
Zend_Date::ATOM Date according to ATOM (string) 2009-02-13T14:53:27+01:00 2009-02-13T14:53:27+01:00
Zend_Date::COOKIE Date for Cookies (string, for Cookies) 2009-02-13T14:53:27+01:00 Friday, 13-Feb-09 14:53:27 Europe/Paris
Zend_Date::RFC_822 Date according to RFC 822 (string) 2009-02-13T14:53:27+01:00 Fri, 13 Feb 09 14:53:27 +0100
Zend_Date::RFC_850 Date according to RFC 850 (string) 2009-02-13T14:53:27+01:00 Friday, 13-Feb-09 14:53:27 Europe/Paris
Zend_Date::RFC_1036 Date according to RFC 1036 (string) 2009-02-13T14:53:27+01:00 Fri, 13 Feb 09 14:53:27 +0100
Zend_Date::RFC_1123 Date according to RFC 1123 (string) 2009-02-13T14:53:27+01:00 Fri, 13 Feb 2009 14:53:27 +0100
Zend_Date::RSS Date for RSS Feeds (string) 2009-02-13T14:53:27+01:00 Fri, 13 Feb 2009 14:53:27 +0100
Zend_Date::W3C Date for HTML/HTTP according to W3C (string) 2009-02-13T14:53:27+01:00 2009-02-13T14:53:27+01:00

Especially note Zend_Date::DATES, since this format specifier has a unique property within Zend_Date as an input format specifier. When used as an input format for $part, this constant provides the most flexible acceptance of a variety of similar date formats. Heuristics are used to automatically extract dates from an input string and then "fix" simple errors in dates (if any), such as swapping of years, months, and days, when possible.

Таблица 9.15. Date and Time Formats (format varies by locale)

Constant Description Date Affected part/example
Zend_Date::ERA Epoch (string, localized, abbreviated) 2009-02-06T14:53:27+01:00 AD (anno Domini)
Zend_Date::ERA_NAME Epoch (string, localized, complete) 2009-02-06T14:53:27+01:00 anno domini (anno Domini)
Zend_Date::DATES Standard date (string, localized, default value). 2009-02-13T14:53:27+01:00 13.02.2009
Zend_Date::DATE_FULL Complete date (string, localized, complete) 2009-02-13T14:53:27+01:00 Friday, 13. February 2009
Zend_Date::DATE_LONG Long date (string, localized, long) 2009-02-13T14:53:27+01:00 13. February 2009
Zend_Date::DATE_MEDIUM Normal date (string, localized, normal) 2009-02-13T14:53:27+01:00 13.02.2009
Zend_Date::DATE_SHORT Abbreviated Date (string, localized, abbreviated) 2009-02-13T14:53:27+01:00 13.02.09
Zend_Date::TIMES Standard time (string, localized, default value) 2009-02-13T14:53:27+01:00 14:53:27
Zend_Date::TIME_FULL Complete time (string, localized, complete) 2009-02-13T14:53:27+01:00 14:53 Uhr CET
Zend_Date::TIME_LONG Long time (string, localized, Long) 2009-02-13T14:53:27+01:00 14:53:27 CET
Zend_Date::TIME_MEDIUM Normal time (string, localized, normal) 2009-02-13T14:53:27+01:00 14:53:27
Zend_Date::TIME_SHORT Abbreviated time (string, localized, abbreviated) 2009-02-13T14:53:27+01:00 14:53

9.6.3. Self-Defined OUTPUT Formats with ISO

If you need a date format not shown above, then use a self-defined format composed from the ISO format token specifiers below. The following examples illustrate the usage of constants from the table below to create self-defined ISO formats. The format length is unlimited. Also, multiple usage of format constants is allowed.

The accepted format specifiers can be changed from ISO Format to PHP's date format if you are more comfortable with it. However, not all formats defined in the ISO norm are supported with PHP's date format specifiers. Use the Zend_Date::setOptions(array('format_type' => 'php')) method to switch Zend_Date methods from supporting ISO format specifiers to PHP date() type specifiers (see Раздел 9.6.4, «Self-defined OUTPUT formats using PHP's date() format specifiers» below).

Пример 9.15. Example usage for self-defined ISO formats

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

$locale = new Zend_Locale('de_AT');
$date = new Zend_Date(1234567890, false, $locale);
print $date->toString("'Era:GGGG='GGGG, ' Date:yy.MMMM.dd'yy.MMMM.dd");
            

Таблица 9.16. Constants for ISO 8601 date output

Constant Description Corresponds best to Affected part/example
G Epoch, localized, abbreviated Zend_Date::ERA AD
GG Epoch, localized, abbreviated Zend_Date::ERA AD
GGG Epoch, localized, abbreviated Zend_Date::ERA AD
GGGG Epoch, localized, complete Zend_Date::ERA_NAME anno domini
GGGGG Epoch, localized, abbreviated Zend_Date::ERA a
y Year, at least one digit Zend_Date::YEAR 9
yy Year, at least two digit Zend_Date::YEAR_SHORT 09
yyy Year, at least three digit Zend_Date::YEAR 2009
yyyy Year, at least four digit Zend_Date::YEAR 2009
yyyyy Year, at least five digit Zend_Date::YEAR 02009
Y Year according to ISO 8601, at least one digit Zend_Date::YEAR_8601 9
YY Year according to ISO 8601, at least two digit Zend_Date::YEAR_SHORT_8601 09
YYY Year according to ISO 8601, at least three digit Zend_Date::YEAR_8601 2009
YYYY Year according to ISO 8601, at least four digit Zend_Date::YEAR_8601 2009
YYYYY Year according to ISO 8601, at least five digit Zend_Date::YEAR_8601 02009
M Month, one or two digit Zend_Date::MONTH_SHORT 2
MM Month, two digit Zend_Date::MONTH 02
MMM Month, localized, abbreviated Zend_Date::MONTH_NAME_SHORT Feb
MMMM Month, localized, complete Zend_Date::MONTH_NAME February
MMMMM Month, localized, abbreviated, one digit Zend_Date::MONTH_NAME_NARROW F
w Week, one or two digit Zend_Date::WEEK 5
ww Week, two digit Zend_Date::WEEK 05
d Day of the month, one or two digit Zend_Date::DAY_SHORT 9
dd Day of the month, two digit Zend_Date::DAY 09
D Day of the year, one, two or three digit Zend_Date::DAY_OF_YEAR 7
DD Day of the year, two or three digit Zend_Date::DAY_OF_YEAR 07
DDD Day of the year, three digit Zend_Date::DAY_OF_YEAR 007
E Day of the week, localized, abbreviated, one char Zend_Date::WEEKDAY_NARROW M
EE Day of the week, localized, abbreviated, two char Zend_Date::WEEKDAY_NAME Mo
EEE Day of the week, localized, abbreviated, three char Zend_Date::WEEKDAY_SHORT Mon
EEEE Day of the week, localized, complete Zend_Date::WEEKDAY Monday
EEEEE Day of the week, localized, abbreviated, one digit Zend_Date::WEEKDAY_NARROW M
e Number of the day, one digit Zend_Date::WEEKDAY_NARROW 4
ee Number of the day, two digit Zend_Date::WEEKDAY_NARROW 04
a Time of day, localized Zend_Date::MERIDIEM vorm.
h Hour, (1-12), one or two digit Zend_Date::HOUR_SHORT_AM 2
hh Hour, (01-12), two digit Zend_Date::HOUR_AM 02
H Hour, (0-23), one or two digit Zend_Date::HOUR_SHORT 2
HH Hour, (00-23), two digit Zend_Date::HOUR 02
m Minute, (0-59), one or two digit Zend_Date::MINUTE_SHORT 2
mm Minute, (00-59), two digit Zend_Date::MINUTE 02
s Second, (0-59), one or two digit Zend_Date::SECOND_SHORT 2
ss Second, (00-59), two digit Zend_Date::SECOND 02
S Millisecond Zend_Date::MILLISECOND 20536
z Time zone, localized, abbreviated Zend_Date::TIMEZONE CET
zz Time zone, localized, abbreviated Zend_Date::TIMEZONE CET
zzz Time zone, localized, abbreviated Zend_Date::TIMEZONE CET
zzzz Time zone, localized, complete Zend_Date::TIMEZONE_NAME Europe/Paris
Z Difference of time zone Zend_Date::GMT_DIFF +0100
ZZ Difference of time zone Zend_Date::GMT_DIFF +0100
ZZZ Difference of time zone Zend_Date::GMT_DIFF +0100
ZZZZ Difference of time zone, separated Zend_Date::GMT_DIFF_SEP +01:00
A Millisecond Zend_Date::MILLISECOND 20563

[Замечание] Замечание

Note that the default ISO format differs from PHP's format which can be irritating if you have not used in previous. Especially the format specifiers for Year and Minute are often not used in the intended way.

For year there are two specifiers available which are often mistaken. The Y specifier for the ISO year and the y specifier for the real year. The difference is small but significant. Y calculates the ISO year, which is often used for calendar formats. See for example the 31. December 2007. The real year is 2007, but it is the first day of the first week in the week 1 of the year 2008. So, if you are using 'dd.MM.yyyy' you will get '31.December.2007' but if you use 'dd.MM.YYYY' you will get '31.December.2008'. As you see this is no bug but a expected behaviour depending on the used specifiers.

For minute the difference is not so big. ISO uses the specifier m for the minute, unlike PHP which uses i. So if you are getting no minute in your format check if you have used the right specifier.

9.6.4. Self-defined OUTPUT formats using PHP's date() format specifiers

If you are more comfortable with PHP's date format specifier than with ISO format specifiers, then you can use the Zend_Date::setOptions(array('format' => 'php')) method to switch Zend_Date methods from supporting ISO format specifiers to PHP date() type specifiers. Afterwards, all format parameters must be given with PHP's date() format specifiers . The PHP date format lacks some of the formats supported by the ISO Format, and vice-versa. If you are not already comfortable with it, then use the standard ISO format instead. Also, if you have legacy code using PHP's date format, then either manually convert it to the ISO format using Zend_Locale_Format::convertPhpToIsoFormat() , or use setOptions(). The following examples illustrate the usage of constants from the table below to create self-defined formats.

Пример 9.16. Example usage for self-defined formats with PHP specifier

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

$locale = new Zend_Locale('de_AT');
Zend_Date::setOptions(array('format_type' => 'php'));
$date = new Zend_Date(1234567890, false, $locale);

// outputs something like 'February 16, 2007, 3:36 am'
print $date->toString('F j, Y, g:i a');

print $date->toString("'Format:D M j G:i:s T Y='D M j G:i:s T Y");
            

The following table shows the list of PHP date format specifiers with their equivalent Zend_Date constants and CLDR/ISO equivalent format specifiers. In most cases, when the CLDR/ISO format does not have an equivalent format specifier, the PHP format specifier is not altered by Zend_Locale_Format::convertPhpToIsoFormat(), and the Zend_Date methods then recognize these "peculiar" PHP format specifiers, even when in the default "ISO" format mode.

Таблица 9.17. Constants for PHP date output

Constant Description Corresponds best to closest CLDR equivalent Affected part/example
d Day of the month, two digit Zend_Date::DAY dd 09
D Day of the week, localized, abbreviated, three digit Zend_Date::WEEKDAY_SHORT EEE Mon
j Day of the month, one or two digit Zend_Date::DAY_SHORT d 9
l (lowercase L) Day of the week, localized, complete Zend_Date::WEEKDAY EEEE Monday
N Number of the weekday, one digit Zend_Date::WEEKDAY_8601 e 4
S English suffixes for day of month, two chars no equivalent no equivalent st
w Number of the weekday, 0=sunday, 6=saturday Zend_Date::WEEKDAY_DIGIT no equivalent 4
z Day of the year, one, two or three digit Zend_Date::DAY_OF_YEAR D 7
W Week, one or two digit Zend_Date::WEEK w 5
F Month, localized, complete Zend_Date::MONTH_NAME MMMM February
m Month, two digit Zend_Date::MONTH MM 02
M Month, localized, abbreviated Zend_Date::MONTH_NAME_SHORT MMM Feb
n Month, one or two digit Zend_Date::MONTH_SHORT M 2
t Number of days per month, one or two digits Zend_Date::MONTH_DAYS no equivalent 30
L Leapyear, boolean Zend_Date::LEAPYEAR no equivalent true
o Year according to ISO 8601, at least four digit Zend_Date::YEAR_8601 YYYY 2009
Y Year, at least four digit Zend_Date::YEAR yyyy 2009
y Year, at least two digit Zend_Date::YEAR_SHORT yy 09
a Time of day, localized Zend_Date::MERIDIEM a (sort of, but likely to be uppercase) vorm.
A Time of day, localized Zend_Date::MERIDIEM a (sort of, but no guarantee that the format is uppercase) VORM.
B Swatch internet time Zend_Date::SWATCH no equivalent 1463
g Hour, (1-12), one or two digit Zend_Date::HOUR_SHORT_AM h 2
G Hour, (0-23), one or two digit Zend_Date::HOUR_SHORT H 2
h Hour, (01-12), two digit Zend_Date::HOUR_AM hh 02
H Hour, (00-23), two digit Zend_Date::HOUR HH 02
i Minute, (00-59), two digit Zend_Date::MINUTE mm 02
s Second, (00-59), two digit Zend_Date::SECOND ss 02
e Time zone, localized, complete Zend_Date::TIMEZONE_NAME zzzz Europe/Paris
I Daylight Zend_Date::DAYLIGHT no equivalent 1
O Difference of time zone Zend_Date::GMT_DIFF Z or ZZ or ZZZ +0100
P Difference of time zone, separated Zend_Date::GMT_DIFF_SEP ZZZZ +01:00
T Time zone, localized, abbreviated Zend_Date::TIMEZONE z or zz or zzz CET
Z Time zone offset in seconds Zend_Date::TIMEZONE_SECS no equivalent 3600
c Standard Iso format output Zend_Date::ISO_8601 no equivalent 2004-02-12T15:19:21+00:00
r Standard Rfc 2822 format output Zend_Date::RFC_2822 no equivalent Thu, 21 Dec 2000 16:01:07 +0200
U Unix timestamp Zend_Date::TIMESTAMP no equivalent 15275422364

    Поддержать сайт на родительском проекте КГБ