phpzend-frameworkzend-datezend-locale

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?


Solution

  • [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.