The methods add(), sub(),
compare(), get(), and
set() operate generically on dates. In each case, the
operation is performed on the date held in the instance object. The
$date operand is required for all of these methods, except
get(), and may be a Zend_Date instance
object, a numeric string, or an integer. These methods assume $date
is a timestamp, if it is not an object. However, the $part operand
controls which logical part of the two dates are operated on, allowing operations on
parts of the object's date, such as year or minute, even when $date
contains a long form date string, such as, "December 31, 2007 23:59:59". The result of
the operation changes the date in the object, except for
compare(), and get().
Example 166. Operating on Parts of Dates
<?php
$date = new Zend_Date(); // $date's timestamp === time()
// changes $date by adding 12 hours
$date->add('12', Zend_Date::HOUR);
print $date;
Convenience methods exist for each combination of the basic operations and several
common date parts as shown in the tables below. These convenience methods help us lazy
programmers avoid having to type out the date
part constants when using the general methods above. Conveniently, they are
named by combining a prefix (name of a basic operation) with a suffix (type of date
part), such as addYear(). In the list below, all combinations
of "Date Parts" and "Basic Operations" exist. For example, the operation "add" exists
for each of these date parts, including addDay(),
addYear(), etc.
These convenience methods have the same equivalent functionality as the basic operation
methods, but expect string and integer $date operands containing only
the values representing the type indicated by the suffix of the convenience method.
Thus, the names of these methods (e.g. "Year" or "Minute") identify the units of the
$date operand, when $date is a string or integer.
Table 46. Date Parts
| Date Part | Explanation |
|---|---|
| Timestamp | UNIX timestamp, expressed in seconds elapsed since January 1st, 1970 00:00:00 GMT. |
| Year | Gregorian calendar year (e.g. 2006) |
| Month | Gregorian calendar month (1-12, localized names supported) |
| 24 hour clock | Hours of the day (0-23) denote the hours elapsed, since the start of the day. |
| minute | Minutes of the hour (0-59) denote minutes elapsed, since the start of the hour. |
| Second | Seconds of the minute (0-59) denote the elapsed seconds, since the start of the minute. |
| millisecond |
Milliseconds denote thousandths of a second (0-999).
Zend_Date supports two additional methods
for working with time units smaller than seconds. By default,
Zend_Date instances use a precision
defaulting to milliseconds, as seen using
getFractionalPrecision(). To change the
precision use
setFractionalPrecision($precision).
However, precision is limited practically to microseconds, since
Zend_Date uses microtime().
|
| Day |
Zend_Date::DAY_SHORT is extracted from
$date if the $date operand is
an instance of Zend_Date or a numeric string.
Otherwise, an attempt is made to extract the day according to the
conventions documented for these constants:
Zend_Date::WEEKDAY_NARROW,
Zend_Date::WEEKDAY_NAME,
Zend_Date::WEEKDAY_SHORT,
Zend_Date::WEEKDAY (Gregorian calendar
assumed)
|
| Week |
Zend_Date::WEEK is extracted from
$date if the $date operand is
an instance of Zend_Date or a numeric string.
Otherwise an exception is raised. (Gregorian calendar assumed)
|
| Date |
Zend_Date::DAY_MEDIUM is extracted from
$date if the $date operand is
an instance of Zend_Date. Otherwise, an
attempt is made to normalize the $date string
into a Zend_Date::DATE_MEDIUM formatted date.
The format of Zend_Date::DAY_MEDIUM depends on
the object's locale.
|
| Weekday |
Weekdays are represented numerically as 0 (for Sunday) through 6
(for Saturday). Zend_Date::WEEKDAY_DIGIT is
extracted from $date, if the
$date operand is an instance of
Zend_Date or a numeric string. Otherwise, an
attempt is made to extract the day according to the conventions
documented for these constants:
Zend_Date::WEEKDAY_NARROW,
Zend_Date::WEEKDAY_NAME,
Zend_Date::WEEKDAY_SHORT,
Zend_Date::WEEKDAY (Gregorian calendar
assumed)
|
| DayOfYear |
In Zend_Date, the day of the year represents
the number of calendar days elapsed since the start of the year
(0-365). As with other units above, fractions are rounded down to
the nearest whole number. (Gregorian calendar assumed)
|
| Arpa |
Arpa dates (i.e. RFC 822 formatted dates) are
supported. Output uses either a "GMT" or "Local differential
hours+min" format (see section 5 of RFC 822).
Before PHP 5.2.2, using the
DATE_RFC822 constant with
PHP date functions sometimes produces incorrect
results. Zend_Date's results are
correct. Example: Mon, 31 Dec 06 23:59:59 GMT
|
| Iso | Only complete ISO 8601 dates are supported for output. Example: 2009-02-14T00:31:30+01:00 |
The basic operations below can be used instead of the convenience operations for
specific date parts, if the
appropriate constant
is used for the $part parameter.
Table 47. Basic Operations
| Basic Operation | Explanation |
|---|---|
get() |
Use Behaviour of get()
Unlike |
set() |
Sets the |
add() |
Adds the |
sub() |
Subtracts the |
copyPart() |
Returns a cloned object, with only |
compare() |
compares |




