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

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_type' => '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.

Example 179. Self-Defined Formats with PHP Specifier

<?php
$locale 
= new Zend_Locale('de_AT');
Zend_Date::setOptions(array('format_type' => 'php'));
$date = new Zend_Date(1234567890false$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");

PHP Date format and using constants

It is important to note that Zend_Date's constants are using the ISO notation. This means, that when you set Zend_Date to use the PHP notation, you should not use Zend_Date's constants, but define the wished format manually. If you don't follow this recommendation, you can get unexpected results.

The following table shows the list of PHP date format specifiers with their equivalent Zend_Date constants and CLDR and ISO equivalent format specifiers. In most cases, when the CLDR and 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.

Table 62. Constants for PHP Date Output

Constant Description Corresponds best to closest CLDR equivalent Result
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-13T15: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

Zend Framework