19
Listing 14.19: Localization of dates using Zend_Date
class Zend_View_Helper_displayDate
{
protected $_view;
protected $_locale;
function setView($view)
{
$this->_view = $view;
$this->_locale = new Zend_Locale($view->localeString); 1
}
function displayDate($dateString, $format = Zend_Date::DATE_LONG)
{
$date = new Zend_Date($dateString, null, $this->_locale); 2
return $date->get($format); 3
}
}
1 Create and store the locale
2 Created Zend_Date object
3 Get the date as a formatted string
As we already know, if a view helper has a setView() function, then the view will call it before calling the
view helper??™s main function. The setView() function is also only called once and so is ideal for creating a
Zend_Locale object from the language string and storing as a variable in the class(#1). Display of a localeaware
date is a case of creating the Zend_Date object (#2) from the date to be displayed and then calling get()
(#3). The get() function takes a format parameter that can be a string or a Zend_Date constant. These constants
are locale aware so that DATE_LONG will display the month name in the correct language. Similarly,
Zend_Date::DATE_SHORT knows that the format of the short date is dd/mm/yy in the UK and mm/dd/yy in
the USA. In general, I recommend staying away from short dates though as they can lead to confusion by users
who aren??™t used to locale aware websites.
Pages:
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366