6.2. Basic Methods

From this chapter on speaking of dates always implies a date with a time, even when not explicitly mentioned. Setting only a specific date implies a time set to 00:00:00. Setting only a specific date implies a date internally set to 01.01.1970. The following sections show basic usage of Zend_Date primarily by example.

6.2.1. The current date

Without any arguments, constructing an instance returns an object in the default locale with the current date.

Example 6.2. Creating the current date

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

$date = new Zend_Date();

// Output of the current timestamp
print $date;
?>

6.2.2. Zend_Date by Example

Reviewing basic methods of Zend_Date is a good place to start for those unfamiliar with date objects. A small example will be provided for each method.

6.2.2.1. Ouput a Date

The date in a Zend_Date object may be obtained as a localized integer or string using the get() method. There are many available options, which will be explained in later sections.

Example 6.3. get() - output a date

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

$date = new Zend_Date();

// Output of the desired date
print $date->get();
?>

6.2.2.2. Setting a Date

The set() method alters the date stored in the object. Again, there are many options which will be explored in later sections.

Example 6.4. set() - set a date

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

$date = new Zend_Date();

// Setting of a new time
$date->set('13:00:00',Zend_Date::TIMES);
print $date->get(Zend_Date::W3C);
?>

6.2.2.3. Adding and Subtracting Dates

Adding two dates with add() usually involves adding a real date in time with an artificial date part, such as 12 hours, as shown in the example below. Both add() and sub() use the same set of options as set(), which will be explained later.

Example 6.5. add() - adding dates

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

$date = new Zend_Date();

// changes $date by adding 12 hours
$date->add('12:00:00', Zend_Date::TIMES);
print $date->get(Zend_Date::W3C);
?>

6.2.2.4. Comparation of dates

All basic Zend_Date methods can operate on entire dates contained in the objects, or can operate on date parts, such as comparing the minutes value in a date to an absolute value. For example, the current minutes in the current time may be compared with a specific number of minutes using compare(), as in the example below.

Example 6.6. compare() - compare dates

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

$date = new Zend_Date();

// Comparation of both times
if ($date->compare(10, Zend_Date::MINUTE) == -1) {
    print "This hour is less than 10 minutes old";
} else {
    print "This hour is at least 10 minutes old";
}
?>

For simple equality comparisons, use equals(), which returns a boolean.

Example 6.7. equals() - identify a date or date part

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

$date = new Zend_Date();

// Comparation of the two dates
if ($date->equals(10, Zend_Date::HOUR)) {
    print "It's 10 o'clock. Time to get to work.";
} else {
    print "It is not 10 o'clock. You can keep sleeping.";
}
?>