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

Create a Date from Database

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 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 convenient way of creating dates from database values.

Example 175. 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($unixtimestampZend_Date::TIMESTAMP);

Example 176. 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($datecolumnZend_Date::ISO_8601);

Zend Framework