简体   繁体   English

如何将ZF2 DateSelect“ day”元素格式化为2位?

[英]How to format ZF2 DateSelect “day” element to 2 digits?

I am wanting to do a date of birth composite drop-down for date of birth (ie three drop-downs - one for day, one for month, one for year). 我想对出生日期做一个出生日期组合下拉列表(即三个下拉列表-一个下拉列表,一天,一个月份,一个年份)。 Helpfully, ZF2 comes with this element built in, called DateSelect 有用的是,ZF2内置了一个名为DateSelect元素。

Here is how I am adding it to the form: 这是我将其添加到表单的方式:

    $this->add([
        'name' => 'date_of_birth',
        'type' => 'DateSelect',
        'attributes' => [
            'required' => true,
            'value' => date('Y-m-d', strtotime('18 years ago')),
        ],
        'options' => [
            'label' => 'Date of Birth',
            'max_year' => date('Y', strtotime('18 years ago')),
        ],
    ]);

To render the form I am doing: 要渲染我正在做的表单:

echo $this->form($form);

Anyway, the day element is being rendered in j format (ie day of the month without leading zeros 1 to 31 ). 无论如何, day元件被渲染在j格式 (该月的当天,即没有前导零131 )。

How do I tell the element to render the day in d format (ie day of the month, 2 digits with leading zeros 01 to 31 )? 如何告诉元素以d格式呈现日期(即月份中的日期,前导零为0131 2位数字)?

I have check source code from Zend\\Form\\Element\\DateSelect and it's view helper Zend\\Form\\View\\Helper\\FormDateSelect . 我从Zend\\Form\\Element\\DateSelect检查了源代码,它是视图助手Zend\\Form\\View\\Helper\\FormDateSelect The rendering date is on view helper, but it didn't use date('j') . 渲染日期位于视图助手上,但未使用date('j') It use IntlDateFormatter to format date, and it will output date format based on locale date format pattern given. 它使用IntlDateFormatter格式化日期,并根据给定的locale日期格式模式输出日期格式。

https://github.com/zendframework/zend-form/blob/release-2.2.0/src/View/Helper/FormDateSelect.php#L90 https://github.com/zendframework/zend-form/blob/release-2.2.0/src/View/Helper/FormDateSelect.php#L90

So, the solution we should pass the locale date format that use two digits day ( dd ). 因此,该解决方案我们应该传递使用两位数日( dd )的语言环境日期格式。 For country date format list, can be check here 有关国家日期格式列表,请在此处查看

https://en.wikipedia.org/wiki/Date_format_by_country https://zh.wikipedia.org/wiki/日期格式_by_country

Based on the date format on list above, India use DD-MM-YY as date format. 根据上面列出的日期格式, 印度使用DD-MM-YY作为日期格式。 we can use this locale to get two digit day format,. 我们可以使用此locale获取两位数的日期格式。

So, please use this on your view helper 因此,请在您的视图助手中使用它

echo $this->formDateSelect($form->get('date_of_birth'), IntlDateFormatter::MEDIUM, 'en_IN');

The view helper Zend\\Form\\View\\Helper\\FormDateSelect has a function getPattern that generates the patten by using the IntlDateFormatter class: 视图帮助程序Zend\\Form\\View\\Helper\\FormDateSelect具有函数getPattern ,该函数通过使用IntlDateFormatter类来生成IntlDateFormatter

$intl           = new IntlDateFormatter($this->getLocale(), $this->dateType, IntlDateFormatter::NONE);
$this->pattern  = $intl->getPattern();

There did not appear to be any method of setting a pattern at element level, so I have extended this view helper allowing me to specify my own pattern: 似乎没有在元素级别设置模式的任何方法,因此我扩展了此视图帮助程序,以允许我指定自己的模式:

<?php
namespace RPK\Form\View\Helper;

use Zend\Form\View\Helper\FormDateSelect as ZendFormDateSelect;

class FormDateSelect extends ZendFormDateSelect
{
    /**
     * Parse the pattern
     *
     * @param  bool $renderDelimiters
     * @return array
     */
    protected function parsePattern($renderDelimiters = true)
    {
        //Replace `day` value of "d" with "dd"
        $pattern = parent::parsePattern($renderDelimiters);
        if($pattern['day'] === 'd') {
            $pattern['day'] = 'dd';
        }
        return $pattern;
    }
}

Very basically, this takes the returned pattern array ( ['day'=>'d', 'month' => 'MMMM', 'year' => 'yyyy'] ), checks if the value of day is a single letter "d" and, if so, replaces it with a double-d. 从根本上讲,这采用返回的模式数组( ['day'=>'d', 'month' => 'MMMM', 'year' => 'yyyy'] ),检查day的值是否为单个字母“ d”,如果是,则将其替换为double -d。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM