Joomla 2.5 calendar field type in custom form date and time selection

别说谁变了你拦得住时间么 提交于 2019-12-03 22:11:53

When clicking on the calendar icon, the calendar widget tries to position itself on the date contained in the corresponding text field. As 0000-00-00 is an invalid date, the Date.parseDate function in media/system/js/calendar-uncompressed.js tries to guess the date from all components of the format string. 0000-00-00 09:30:00 is recognized as Sep 30, because 09 < 12, so it looks like a month number, and thus it returns Sep 30, 9:30. On the other hand, 0000-00-00 19:30:00 is not recognized as any valid date, and the function returns today. Hence the difference in the time part.

If you look at the XML form files for com_content for example, you'll see that they don't use default values for the calendar fields.

You could however create a custom field type derived from JFormFieldCalendar which would give you full flexibility. For example:

forms/whatever.xml

<field name="f_start" type="PubDateCalendar"
       format="%Y-%m-%d %H:%M:%S" default="start" ... />
<field name="f_end" type="PubDateCalendar"
       format="%Y-%m-%d %H:%M:%S" default="end" ... />

fields/pubdatecalendar.php

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

class JFormFieldPubDateCalendar extends JFormFieldCalendar
{
    public $type = 'PubDateCalendar';
    protected function getInput()
    {
        $format = $this->element['format']
                ? (string) $this->element['format']
                : '%Y-%m-%d';
        if ($this->element['default'] == 'start') {
            $this->value = strftime($format);
        } else if ($this->element['default'] == 'end') {
            $this->value = strftime($format, time() + 28 * 24 * 60 * 60);
        }
        return parent::getInput();
    }
}
?>

or whatever suits your particular application better.

Maybe this will be helpful for someone like me who was looking for answer on this question but on current version of Joomla 3.3.6:

<field
  name="birth_day"
  type="calendar"
  label="COM_PERSONS_PERSON_FIELD_BIRTHDAY_LABEL"
  description="COM_PERSONS_PERSON_FIELD_BIRTHDAY_DESC"
  class="inputbox" 
  size="20"
  format="%Y-%m-%d"
  filter="user_utc" />
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!