Zend_Date / Zend_Locale : Change date format for a specific locale?

Is it possible to instruct Zend_Locale that it should use a certain locale, except for a minor alteration to the date format? I don't want to use the Zend_Date::toString() with the specific formatting, because that would use this format on all locales.

Case in question: I have dates formatted according to a user's locale setting. My fellow Dutch (nl_NL) users are asking for dd-mm-yyyy formatted dates, instead of dd-mm-yy which Zend_Locale vehemently claims to be our short date format. If I change the code where the date is outputted to explicitly use a custom format, that applies to all customers instead of just the cheeseheads. I could check the user's locale, but if more exceptions need to be created, every time a date is echo'd I'd need to add these checks and exceptions, the prospect of which makes me cringe.

I can't alter the Zend_Locale XML data directly (and don't want to), as the ZF library is used by various sites.

I'm sure this is one of those "really simple" issues... once you know how. Any ideas?

Answers


[Trying a new answer since this approach is substantively different than the previous answer.]

I assume that you would be calling Zend_Date::toString() in a view.

So, maybe you could store a list of format overrides in a config file. During Bootstrap, check if the loaded locale requires an override and then store the format in the view. Then whenever you output a date, use that format.

Something like this:

In application/configs/application.ini:

dateFormat.nl_NL = "d-m-Y"

In application/Bootstrap.php:

protected function _initDateFormat()
{
    // Bootstrap and grab the view
    $this->bootstrap('view');
    $view = $this->getResource('view');

    // grab the date format overrides from options or null for locale-default
    $options = $this->getOptions();
    $this->bootstrap('locale');
    $locale = $this->getResource('locale');
    $dateFormat = isset($options['dateFormat'][$locale]) 
        ? $options['dateFormat'][$locale] 
        : null;

    // stash the dateFormat into the view
    $view->dateFormat = $dateFormat;
}

Finally in a view-script, where $date is a Zend_Date object:

<p>The date is <?= $date->toString($this->dateFormat) ?>.</p>

If $view->dateFormat is null, then the format for the current locale will be used. Otherwise, your override will apply.


There must be some way to:

  1. Extend Zend_Locale
  2. Create a new locale corresponding to the existing locale whose behavior you want to customize.
  3. Create a new XML file for the new locale containing the customizations.

Then, in Bootstrap, create the new locale object. If the current locale matches the locale you wish to customize, then manually switch to the new locale.

The key is probably where to place the new XML files and how to define the mapping. Of course, you probably don't want to place anything new in the Zend folders, so how do we tell Zend_Locale and his friends to use the new location? Is that mapping information "hard-coded" in the various Zend_Locale_XXX classes?

Anyway, just talking out some ideas. Hope it gives you some direction or inspires someone else more knowledgeable than me - a rather low bar, indeed! - to come up with something more definitive and authoritative.

Cheers!


I was also commencing to consider David Weinraub's second solution.

Here is my flavour for switching to 4-digits years for any Locale:

In Bootstrap class (application/Bootstrap.php file)

protected function _initDate() {
    /** Test and correct Zend_Date::DATE_SHORT format **/
    $localeDataDateFormats = Zend_Locale_Data::getList(Zend_Locale::findLocale(), 'date'); // Date formats for auto Locale (@see Zend_Locale::findLocale())
    define(
        'DATE_SHORT_LONGYEAR',  // Define our new format, should now use it instead of Zend_Date::DATE_SHORT
        preg_replace('@((' . Zend_Date::YEAR . ')+|(' . Zend_Date::YEAR_8601 . ')+)@', '$2$3', $localeDataDateFormats['short']) // Makes years displayed with every digits available
    );
    /** /Test and correct Zend_Date::DATE_SHORT format **/
}

Use it (use the new constant instead of Zend_Date::DATE_SHORT):

$date = new Zend_Date();
echo $date->toString(DATE_SHORT_LONGYEAR);

Need Your Help

Piping with an array of commands

c fork pipe execvp dup

I have a function that takes an array of strings and the size of the array. I have a loop that tokenizes the string into command and arguements and then forks and executes the commands one by one. ...

Skype events listener for android

android skype skype4java

Is there any Skype listener in android?? like phoneStateListener which listen the events of phone call either it is in offhook, on call, idle state etc.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.