9.5. Creation of dates

Zend_Date provides several different ways to create a new instance of itself. As there are different needs the most convinient ways will be shown in this chapter.

9.5.1. Create the actual date

The simplest way of creating a date object is to create the actual date. You can eigther create a new instance with new Zend_Date() or use the convinient 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.

Пример 9.10. 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 Zend_Date instance.

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

$date = new Zend_Date();
            

Пример 9.11. 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.

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

$date = Zend_Date::now();
            

9.5.2. Create a date from database

Databases are often used to store date values. But the problem is, that every database outputs it's date values in a different way. MsSQL databases use a quite different standard date output than MySQL databases. But for simplification 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 convinient way of creating dates from database values.

Пример 9.12. Quick creation of dates from database date values

All databases are known to handle queries as fast as possible. They are build 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.

<?php
// SELECT UNIX_TIMESTAMP(my_datetime_column) FROM my_table
require_once 'Zend/Date.php';

$date = new Zend_Date($unixtimestamp, Zend_Date::TIMESTAMP);
            

Пример 9.13. 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.

<?php
// SELECT datecolumn FROM my_table
require_once 'Zend/Date.php';

$date = new Zend_Date($datecolumn, Zend_Date::ISO_8601);
            

9.5.3. Create dates from an array

Dates can also be created by the usage of an array. This is an simple and easy way. The used array values 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

Пример 9.14. 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.

<?php
require_once 'Zend/Date.php';
$datearray = array('year' => 2006, 'month' => 4, 'day' => 18, 'hour' => 12, 'minute' => 3, 'second' => 10);
$date = new Zend_Date($datearray);
            
<?php
require_once 'Zend/Date.php';
$datearray = array('year' => 2006, 'month' => 4, 'day' => 18);
$date = new Zend_Date($datearray);
            

    Поддержать сайт на родительском проекте КГБ