> Zend Framework中文手册 > 9.6. 一般日期函数的常量

9.6.  一般日期函数的常量

不论什么时候 Zend_Date 方法需要参数 $parts,下面常数中其中之一可担当此任, 用来选择日期的特定部分或指示日期使用的或期望使用的格式(例如 RFC 822)。

9.6.1.  使用常数

例如,常数 Zend_Date::HOUR 可以像下列所示来使用。当和星期几、日期、小时、分钟、秒和任何其它因位于世界不同的地方而不同地表示的日期部分一起使用时, 对象的时区将自动用来计算当前值,即使是内部的时间戳和此刻一致并且不论用户的地理位置在世界的何方。 不管使用了哪些单元,输出必需表达为 GMT/UTC 或本地化。下面的输出例子反映了 Europe/GMT+1 hour (e.g. Germany, Austria, France) 的本地化。

表 9.7.  Zend_Date::HOUR 的操作

函数/输入 描述 原始日期 效果/输出
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.  所有常数列表

Zend_Date 中每个日期/时间的部分都有独一无二的常数。所有 Zend_Date 支持的常数如下:

表 9.8. Day Constants

常数 描述 日期 受影响部分/例子
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.  星期常数

常数 描述 日期 受影响部分/例子
Zend_Date::WEEK Week (as a number, 1-53) 2009-02-06T14:53:27+01:00 7

表 9.10.  月常数

常数 描述 日期 受影响部分/例子
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.  年常数

常数 描述 日期 受影响部分/例子
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.  时间常数

常数 描述 日期 受影响部分/例子
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.  时区常数

常数 描述 日期 受影响部分/例子
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.  日期格式常数(格式包括时区)

常数 描述 日期 受影响部分/例子
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

特别注意 Zend_Date::DATES,因为这个格式指示器在 Zend_Date 之内有个唯一的属性作为 输入 格式指示器。 当 $part 用作一个输入格式,该常量提灵活地接受许多相似的日期格式。启发式自动从输入字符串里解析并“修改”日期里的简单的错误(如果有的话),如当需要时,交换年月日的位置。

表 9.15.  日期和时间格式 (format varies by locale)

常数 描述 日期 受影响部分/例子
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.  自定义 ISO 输出格式

如果你需要的日期格式没有在上述的格式中出现,那么使用从下面 ISO 格式指定器产生的自定义格式。 下面的例子示例了从下列表中生成的自定义 ISO 格式的常量的用法。格式长度无限制。并且允许格式常量的多重用法。

如果你喜欢的话,可接受的格式指定器可以从 ISO 格式修改为 PHP 的日期格式,然而,PHP 日期格式指定不支持所有的按 ISO 标准定义的格式。 使用 Zend_Date::setOptions(array('format_type' => 'php')) 方法来把 Zend_Date 方法从支持 ISO 格式制定器切换到 PHP date()类型制定器(参见下面 第 9.6.4 节 “ 使用 PHP 的 date() 格式指定器的自定义输出格式 ”)。

例 9.15.  自定义 ISO 格式用法示例

$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.  ISO 8601 日期输出 常量

常数 描述 日期 受影响部分/例子
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

9.6.  一般日期函数的常量 注意

注意缺省的 ISO 格式不同于 PHP 的格式,如果在先前没有使用,PHP 的格式可以迭代。 特别是 Year and Minute 的格式指定器不常按预期来使用。

对于 year 有两个可用的指定器经常发生错误: ISO 年的Y 指定器和真实年的 y 的指定器。 区别微小但意义重大。 Y 计算 ISO 年,常用于日历格式,例如 the 31. December 2007。 真实年为 2007,但它是 2008 年的第一周的第一天,所以,如果使用 'dd.MM.yyyy' 会得到 '31.December.2007' 但如果使用 'dd.MM.YYYY' 会得到 '31.December.2008'。 正如你所看到的,这不是 bug , 但期望的行为依赖指定器的使用。

minute 来说区别不大。ISO 使用 m 指定器,PHP 使用 i。 所以如果没有按你的格式获得分钟数,检查是否使用了正确的指定器。

9.6.4.  使用 PHP 的 date() 格式指定器的自定义输出格式

如果你更喜欢使用 PHP 的日期格式指定器,可以使用 Zend_Date::setOptions(array('format' => 'php')) 方法 把 Zend_Date 方法从支持 ISO 格式制定器切换到 PHP date()类型制定器。然后所有参数必需用 PHP's date() 格式指定器 给出。 PHP 日期格式缺乏一些 ISO 格式支持的格式,反之亦然。如果不喜欢,可以使用标准的 ISO 格式。 而且,如果你有一些历史遗留的使用 PHP 日期格式的代码,可以用 Zend_Locale_Format::convertPhpToIsoFormat() 手工转换为 ISO 格式,或使用 setOptions()。 下面的例子示例了从下表生成的自定义格式的常量的用法。

例 9.16.  用 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");

            

下标列出了带有相应等同的 Zend_Date常量和 CLDR/ISO 格式指定器的 PHP 日期格式指定器。 对一些情况,当 CLDR/ISO 格式没有等同的格式指定器时,PHP 格式指定器不用 Zend_Locale_Format::convertPhpToIsoFormat() 来改变, 并且 Zend_Date 方法承认这些 “奇特的” PHP 格式指定器,即使是处于缺省的 "ISO" 格式模式中。

表 9.17.  PHP 日期输出 常量

常数 描述 Corresponds best to closest CLDR equivalent 受影响部分/例子
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