簡體   English   中英

日歷表單字段類型Joomla中的日期格式問題

[英]Date format issues in Calendar form field type Joomla

我在Joomla Calendar form field類型上遇到問題,這是我的代碼:

<field name="date" type="calendar" label="Date (*)"
            class="inputbox required" size="22"
            format="%Y-%m-%d"  labelclass="control-label" readonly="true" default="NOW"  
        />

我想做的是在此字段中將默認值設置為當前日期,但它顯示的是默認值,例如2014-03-07 00:00:00但我不想在此字段中顯示時間,因為您可以看到我定義了格式"%Y-%m-%d" ,但是不知道為什么如果有人對此有任何解決方案,它會像這樣顯示,請提供幫助。

嘗試這個,

格式(可選)是要使用的日期格式。 這是PHP用於指定日期字符串格式的格式(請參見下文)。 如果未給出格式參數,則假定為'%Y-%m-%d'(給出的日期類似於“ 2008-04-16”)。

閱讀更多

如果上述情況不起作用,請嘗試以下方法。

在您的XML中

<field name="date" type="PubDateCalendar" label="Date (*)"
            class="inputbox required" size="22"
            format="%Y-%m-%d"  labelclass="control-label" readonly="true"  
        />

字段/pubdatecalendar.php

<?php
defined('_JEXEC') or die;
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('calendar');

class JFormFieldPubDateCalendar extends JFormFieldCalendar
{
    public $type = 'PubDateCalendar';
    protected function getInput()
    {

        $this->value = date('Y-m-d');

        return parent::getInput();
    }
}
?>

希望它的作品..

改試試這個。。。這段代碼和上面@Jobin Jose解決方案之間的區別是您調用父級。 在JFormFieldCalendar類中查看,它是具有自身格式的ovveride

<?php

defined('_JEXEC') or die;
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('calendar');

class JFormFieldCustomCalendar extends JFormFieldCalendar
{

    public $type = 'CustomCalendar';

    protected $defaultFormat = 'd-m-Y';

    /*
     * Un jour d'intervalle entre le debut et la fin
     */
    protected $interval = 'P1D';

    protected function getInput()
    {
        parent::getInput();

        // Build the attributes array.
        $attributes = array();

        empty($this->size)      ? null : $attributes['size'] = $this->size;
        empty($this->maxlength) ? null : $attributes['maxlength'] = $this->maxlength;
        empty($this->class)     ? null : $attributes['class'] = $this->class;
        !$this->readonly        ? null : $attributes['readonly'] = '';
        !$this->disabled        ? null : $attributes['disabled'] = '';
        empty($this->onchange)  ? null : $attributes['onchange'] = $this->onchange;
        empty($hint)            ? null : $attributes['placeholder'] = $hint;
        $this->autocomplete     ? null : $attributes['autocomplete'] = 'off';
        !$this->autofocus       ? null : $attributes['autofocus'] = '';

        if ($this->required) {
            $attributes['required'] = '';
            $attributes['aria-required'] = 'true';
        }

        $date = new DateTime("now");

        $format = $this->element['format'] ? (string) $this->element['format'] : $this->defaultFormat;
        $validFormat = preg_replace('/%/', '', $format);

        if ($this->element['default'] == 'start') {
            $this->value = $date->format($validFormat);
        } else if ($this->element['default'] == 'end') {
            $date->add(new DateInterval($this->interval));

            $this->value = $date->format($validFormat);
        }

        return JHtml::_('calendar', $this->value, $this->name, $this->id, $format, $attributes);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM