Zend_Date provides several different ways to create a new instance of
itself. As there are different needs the most convenient ways will be shown in this chapter.
The simplest way of creating a date object is to create the actual date. You can either
create a new instance with new Zend_Date() or use the convenient
static method Zend_Date::now() which both will return the
actual date as new instance of
Zend_Date. The actual date always
include the actual date and time for the actual set timezone.
Example #1 Date Creation by Instance
Date creation by creating a new instance means that you do not need to give an
parameter. Of course there are several parameters which will be described later but
normally this is the simplest and most used way to get the actual date as
$date = new Zend_Date();
Example #2 Static Date Creation
Sometimes it is easier to use a static method for date creation. Therefor you can
use the now() method. It returns a
new instance of
Zend_Date the same way as if you would use
new Zend_Date(). But it will always return the actual date and
can not be changed by giving optional parameters.
$date = Zend_Date::now();
Databases are often used to store date values. But the problem is, that every database
outputs its date values in a different way. MsSQL databases use a
quite different standard date output than MySQL databases. But for
Zend_Date makes it very easy to create a date
from database date values.
Of course each database can be said to convert the output of a defined column to a special value. For example you could convert a datetime value to output a minute value. But this is time expensive and often you are in need of handling dates in an other way than expected when creating the database query.
So we have one quick and one convenient way of creating dates from database values.
Example #3 Quick Creation of Dates from Database Date Values
All databases are known to handle queries as fast as possible. They are built to act and respond quick. The quickest way for handling dates is to get unix timestamps from the database. All databases store date values internal as timestamp (not unix timestamp). This means that the time for creating a timestamp through a query is much smaller than converting it to a specified format.
// SELECT UNIX_TIMESTAMP(my_datetime_column) FROM my_table $date = new Zend_Date($unixtimestamp, Zend_Date::TIMESTAMP);
Example #4 Convenient Creation of Dates from Database Date Values
The standard output of all databases is quite different even if it looks the same on the first eyecatch. But all are part of the ISO Standard and explained through it. So the easiest way of date creation is the usage of Zend_Date::ISO_8601. Databases which are known to be recognised by Zend_Date::ISO_8601 are MySQL, MsSQL for example. But all databases are also able to return a ISO-8601 representation of a date column. ISO-8601 has the big advantage that it is human readable. The disadvantage is that ISO-8601 needs more time for computation than a simple unix timestamp. But it should also be mentioned that unix timestamps are only supported for dates after 1 January 1970.
// SELECT datecolumn FROM my_table $date = new Zend_Date($datecolumn, Zend_Date::ISO_8601);
Dates can also be created by the usage of an array. This is a simple and easy way. The used array keys are:
day: day of the date as number
month: month of the date as number
year: full year of the date
hour: hour of the date
minute: minute of the date
second: second of the date
Example #5 Date Creation by Array
Normally you will give a complete date array for creation of a new date instance. But when you do not give all values, the not given array values are zeroed. This means that if f.e. no hour is given the hour 0 is used.
$datearray = array('year' => 2006, 'month' => 4, 'day' => 18, 'hour' => 12, 'minute' => 3, 'second' => 10); $date = new Zend_Date($datearray);
$datearray = array('year' => 2006, 'month' => 4, 'day' => 18); $date = new Zend_Date($datearray);