jump to navigation

How to Handle Dates with Zend-Framework’s Zend_Date Component September 14, 2012

Posted by Tournas Dimitrios in PHP.

Dates and times are quite unlike most other sorts of data you are likely to encounter when programming with PHP 5 . Dates are rather messy with respect to both calculation and representation , and one is about as bad as the other . While PHP has plethora of build in functions to manipulate date and time , ( time() – mktime() – date() )  , these have a time-stamp-range limitation .

From the PHP-manual : The valid range  of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.
For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction .End-of-PHP-manual .

Note that 64-bit PHP (VC9 Thread safe) on Vista 64-bit still uses 32-bit signed integer timestamp range for date values

Two choices are the rescue for the aforementioned time-stamp-range limitation :

  • PHP’s  dateTime Class : You can use the functions of this Class to format the date and time in many different ways . The date and time information is internally stored as a 64-bit number so all conceivably useful dates (including negative years) are supported . The range is from about 292 billion years in the past to the same in the future . No extra installation needed to use this Class , it is part of the PHP core since PHP < 5.2  .
  • ZF’s  Zend_Date module : This component offers a detailed , but simple API for manipulating dates and times . Its methods accept a wide variety of types of information , including date parts , in numerous combinations yielding many features and possibilities above and beyond the existing PHP date related functions . Thanks to the loosely coupled structure of the Zend Framework , this module can be used as a separate component  into other PHP code .

As this article is devoted to ZF’s Date module let’s show some practical examples : 

Before using any date related functions in PHP or Zend Framework , first make certain your application has a correct default timezone, by either setting the TZ environment variable, using the date.timezone php.ini setting , or using » date_default_timezone_set().

Zend_Date has 69 constants defined. It is good to know what each one of them represents . For this purpose , run the following code (on-line version) :

include 'Zend/Date.php' ;
$test = new Zend_Date() ;
$class = new ReflectionClass($test);
$dateConstants = $class->getConstants();
$date = new Zend_Date() ;

echo "14px;margin: 10px;max-width: 99%; border-spacing: 2px;
border-color: red;border:3px solid green;background-color:#EEF5C4;color:#070801;width:400px;\">";
foreach ($dateConstants as $dateConstantKey=>$dateConstantValue)
    echo "<tr>";
    echo "<td>Zend_Date::";
    echo $dateConstantKey;
    echo "</td>";
    echo "<td>";
    echo $date->get($dateConstantValue);
    echo "</td>";
    echo "</tr>";
echo "";

And the result is :

Zend_Date::DAY 14
Zend_Date::DAY_SHORT 14
Zend_Date::DAY_SUFFIX th
Zend_Date::DAY_OF_YEAR 257
Zend_Date::WEEKDAY Friday
Zend_Date::WEEKDAY_8601 5
Zend_Date::WEEK 37
Zend_Date::MONTH 09
Zend_Date::MONTH_SHORT 9
Zend_Date::MONTH_DAYS 30
Zend_Date::MONTH_NAME September
Zend_Date::YEAR 2012
Zend_Date::YEAR_SHORT 12
Zend_Date::YEAR_8601 2012
Zend_Date::YEAR_SHORT_8601 12
Zend_Date::LEAPYEAR 1
Zend_Date::SWATCH 691
Zend_Date::HOUR 18
Zend_Date::HOUR_SHORT 18
Zend_Date::HOUR_AM 06
Zend_Date::HOUR_SHORT_AM 6
Zend_Date::MINUTE 36
Zend_Date::MINUTE_SHORT 36
Zend_Date::SECOND 11
Zend_Date::SECOND_SHORT 11
Zend_Date::TIMEZONE_NAME Europe/Athens
Zend_Date::DAYLIGHT 1
Zend_Date::GMT_DIFF +0300
Zend_Date::GMT_DIFF_SEP +03:00
Zend_Date::TIMEZONE_SECS 10800
Zend_Date::ISO_8601 2012-09-14T18:36:11+03:00
Zend_Date::RFC_2822 Fri, 14 Sep 2012 18:36:11 +0300
Zend_Date::TIMESTAMP 1347636971
Zend_Date::ERA AD
Zend_Date::ERA_NAME Anno Domini
Zend_Date::ERA_NARROW A.
Zend_Date::DATES Sep 14, 2012
Zend_Date::DATE_FULL Friday, September 14, 2012
Zend_Date::DATE_LONG September 14, 2012
Zend_Date::DATE_MEDIUM Sep 14, 2012
Zend_Date::DATE_SHORT 9/14/12
Zend_Date::TIMES 6:36:11 PM
Zend_Date::TIME_FULL 6:36:11 PM Europe/Athens
Zend_Date::TIME_LONG 6:36:11 PM EEST
Zend_Date::TIME_MEDIUM 6:36:11 PM
Zend_Date::TIME_SHORT 6:36 PM
Zend_Date::DATETIME Sep 14, 2012 6:36:11 PM
Zend_Date::DATETIME_FULL Friday, September 14, 2012 6:36:11 PM Europe/Athens
Zend_Date::DATETIME_LONG September 14, 2012 6:36:11 PM EEST
Zend_Date::DATETIME_MEDIUM Sep 14, 2012 6:36:11 PM
Zend_Date::DATETIME_SHORT 9/14/12 6:36 PM
Zend_Date::ATOM 2012-09-14T18:36:11+03:00
Zend_Date::COOKIE Friday, 14-Sep-12 18:36:11 Europe/Athens
Zend_Date::RFC_822 Fri, 14 Sep 12 18:36:11 +0300
Zend_Date::RFC_850 Friday, 14-Sep-12 18:36:11 Europe/Athens
Zend_Date::RFC_1036 Fri, 14 Sep 12 18:36:11 +0300
Zend_Date::RFC_1123 Fri, 14 Sep 2012 18:36:11 +0300
Zend_Date::RFC_3339 2012-09-14T18:36:11+03:00
Zend_Date::RSS Fri, 14 Sep 2012 18:36:11 +0300
Zend_Date::W3C 2012-09-14T18:36:11+03:00

A couple practical examples :

include 'Zend/Date.php' ;
$date = new Zend_Date();

// Setting of a new time ==2012-09-14T13:00:00+03:00
print $date->set('13:00:00',Zend_Date::TIMES)->get(Zend_Date::W3C)."<br>";
// Unix epoch :  1347616800
print $date->set('13:00:00',Zend_Date::TIMES)->get()."<br>";
// 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.";

The Zend_Date API has a long list of functions for :

  • Common date operations : set() , add() , sub() , compare() , copyPart()
  • Comparing dates : equals() , isEarlier() , isLater() , isToday() , isTomorrow() , isYesterday() , isLeapYear() , isDate()
  • Working with fractions of seconds : getFractionalPrecision() , setFractionalPrecision() 
  • Sunrise / Sunset information :  getSunrise($location) , getSunset($location) , getSunInfo($location)
  • Getting Dates and Date Parts : toArray() Returns an array representation of the selected date , it contains 12 parts of the date (hour , year , timezone , Unix-temestamp …….

Creation of Dates- Objects :

  •  You can either create a new instance with new Zend_Date() or use the convenient static methodZend_Date::now() which both will return the actual date as new instance of Zend_Date .
  • 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.
    // SELECT UNIX_TIMESTAMP(my_datetime_column) FROM my_table
    $date = new Zend_Date($unixtimestamp , Zend_Date::TIMESTAMP); //Unix time format 
    $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.
    $datearray = array(‘year’ => 2006, ‘month’ => 4, ‘day’ => 18);
    $date = new Zend_Date($datearray);


1. linux - September 15, 2012

Normally I don’t read article on blogs, however I would like to say that this write-up very forced me to take a look at and do it! Your writing taste has been amazed me. Thanks, very nice article.

2. The DateTime class in PHP | Sam's PHP - September 15, 2012

[…] How to Handle Dates with Zend-Framework’s Zend_Date Component. Share this:PrintEmailMoreLike this:LikeBe the first to like […]

3. webdesign - December 12, 2012


Thanks for such a great information. In these days its hard to find a honest blog about zend framework developer

This is sooo exciting! I love your blog and check it every day.the blog has grown so much. Wish you & the blog more success!

God bless 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s