Ticket #83 (assigned enhancement)

Opened 1 year ago

Last modified 6 months ago

Request: Solar_View_Helper_DateLocale

Reported by: Rodrigo Moraes <rodrigo.moraes@gmail.com> Assigned to: pmjones (accepted)
Priority: trivial Component: code
Keywords: Solar_View_Helper Cc:

Description

Hi, I use a helper which is much like Solar_View_Helper_Date, but it uses strftime() instead of date:

[Tipos_View_Helper_DateLocale]

Interesting for the international audience; maybe it is worth to add something like this to Solar?

Attachments

Change History

07/07/07 03:52:58 changed by Rodrigo Moraes <rodrigo.moraes@gmail.com>

One strategy - add a _format() method to Solar_View_Helper_Timestamp:

<?php
    protected function _format($format, $timestamp)
    {
        return date($format, $timestamp);
    }

... and return it from _process():

<?php
    protected function _process($spec, $format)
    {
        // ... process offset ...

        // now generate output
        return $this->_view->escape($this->_format($format, $time));
    }

Then Solar_View_Helper_Timestamp / Solar_View_Helper_Date would use the default format using date(), and the locale-aware date helper would define one that uses strftime(), like:

<?php
/**
 *
 * Helper for a formatted and localized date using [[php::strftime() | ]]
 * format codes.
 *
 * @category Tipos
 *
 * @package Tipos_View_Helper
 *
 */
class Tipos_View_Helper_DateLocale extends Solar_View_Helper_Timestamp
{
    /**
     *
     * User-provided configuration values.
     *
     * Keys are...
     *
     * `format`
     * : (string) The default output formatting using [[php:strftime() | ]] codes.
     *
     * @var array
     *
     */
    protected $_Tipos_View_Helper_DateLocale = array(
        'format' => '%d-%b-%Y',
    );

    /**
     *
     * Outputs a formatted date.
     *
     * @param string $spec Any date-time string suitable for
     * strtotime().
     *
     * @param string $format An optional custom [[php::strftime() | ]]
     * formatting string; null by default.
     *
     * @return string The formatted date string.
     *
     */
    public function dateLocale($spec, $format = null)
    {
        return $this->_process($spec, $format);
    }

    /**
     *
     * Returns a formatted date using [[php::strftime() | ]].
     *
     * @param string $format An [[php::strftime() | ]] formatting string.
     *
     * @param int $timestamp A Unix timestamp.
     *
     */
    protected function _format($format, $timestamp)
    {
        return strftime($format, $timestamp);
    }
}

07/11/07 16:54:36 changed by pmjones

  • status changed from new to assigned.

We might get even simpler than that; we can have a "strftime" config key. When true, the class uses strftime(), when false, it uses date(). Would that be a good solution?

01/03/08 05:52:27 changed by moraes

Hey! Sorry, I haven't seen your comment before. Yes, it seems that a config key would be fine. :)

Btw, I have being using this solution for a long time now, and for me it is perfect and very appropriate to get localized dates.

+1 for "strftime" config key. :)

thanks.


Add/Change #83 (Request: Solar_View_Helper_DateLocale)