Back to HTMLHelper class

Method calendar

public static string
calendar
(mixed $value, mixed $name, mixed $id, mixed $format = '%Y-%m-%d', mixed $attribs = array())
Displays a calendar control field
Parameters
  • string $value The date value
  • string $name The name of the text field
  • string $id The id of the text field
  • string $format The date format
  • mixed $attribs Additional HTML attributes The array can have the following keys: readonly Sets the readonly parameter for the input tag disabled Sets the disabled parameter for the input tag autofocus Sets the autofocus parameter for the input tag autocomplete Sets the autocomplete parameter for the input tag filter Sets the filter for the input tag
Returns
  • string HTML markup for a calendar field
Since
  • 1.5
Class: HTMLHelper
Project: Joomla

Method calendar - Source code

/**
 * Displays a calendar control field
 *
 * @param   string  $value    The date value
 * @param   string  $name     The name of the text field
 * @param   string  $id       The id of the text field
 * @param   string  $format   The date format
 * @param   mixed   $attribs  Additional HTML attributes
 *                            The array can have the following keys:
 *                            readonly      Sets the readonly parameter for the input tag
 *                            disabled      Sets the disabled parameter for the input tag
 *                            autofocus     Sets the autofocus parameter for the input tag
 *                            autocomplete  Sets the autocomplete parameter for the input tag
 *                            filter        Sets the filter for the input tag
 *
 * @return  string  HTML markup for a calendar field
 *
 * @since   1.5
 *
 */
public static function calendar($value, $name, $id, $format = '%Y-%m-%d', $attribs = array())
{
    $app = Factory::getApplication();
    $lang = $app->getLanguage();
    $tag = $lang->getTag();
    $calendar = $lang->getCalendar();
    $direction = strtolower($app->getDocument()->getDirection());
    // Get the appropriate file for the current language date helper
    $helperPath = 'system/fields/calendar-locales/date/gregorian/date-helper.min.js';
    if ($calendar && is_dir(JPATH_ROOT . '/media/system/js/fields/calendar-locales/date/' . strtolower($calendar))) {
        $helperPath = 'system/fields/calendar-locales/date/' . strtolower($calendar) . '/date-helper.min.js';
    }
    $readonly = isset($attribs['readonly']) && $attribs['readonly'] === 'readonly';
    $disabled = isset($attribs['disabled']) && $attribs['disabled'] === 'disabled';
    $autocomplete = isset($attribs['autocomplete']) && $attribs['autocomplete'] === '';
    $autofocus = isset($attribs['autofocus']) && $attribs['autofocus'] === '';
    $required = isset($attribs['required']) && $attribs['required'] === '';
    $filter = isset($attribs['filter']) && $attribs['filter'] === '';
    $todayBtn = $attribs['todayBtn'] ?? true;
    $weekNumbers = $attribs['weekNumbers'] ?? true;
    $showTime = $attribs['showTime'] ?? false;
    $fillTable = $attribs['fillTable'] ?? true;
    $timeFormat = $attribs['timeFormat'] ?? 24;
    $singleHeader = $attribs['singleHeader'] ?? false;
    $hint = $attribs['placeholder'] ?? '';
    $class = $attribs['class'] ?? '';
    $onchange = $attribs['onChange'] ?? '';
    $minYear = $attribs['minYear'] ?? null;
    $maxYear = $attribs['maxYear'] ?? null;
    $showTime = $showTime ? "1" : "0";
    $todayBtn = $todayBtn ? "1" : "0";
    $weekNumbers = $weekNumbers ? "1" : "0";
    $fillTable = $fillTable ? "1" : "0";
    $singleHeader = $singleHeader ? "1" : "0";
    // Format value when not nulldate ('0000-00-00 00:00:00'), otherwise blank it as it would result in 1970-01-01.
    if ($value && $value !== Factory::getDbo()->getNullDate() && strtotime($value) !== false) {
        $tz = date_default_timezone_get();
        date_default_timezone_set('UTC');
        $inputvalue = strftime($format, strtotime($value));
        date_default_timezone_set($tz);
    } else {
        $inputvalue = '';
    }
    $data = array('id' => $id, 'name' => $name, 'class' => $class, 'value' => $inputvalue, 'format' => $format, 'filter' => $filter, 'required' => $required, 'readonly' => $readonly, 'disabled' => $disabled, 'hint' => $hint, 'autofocus' => $autofocus, 'autocomplete' => $autocomplete, 'todaybutton' => $todayBtn, 'weeknumbers' => $weekNumbers, 'showtime' => $showTime, 'filltable' => $fillTable, 'timeformat' => $timeFormat, 'singleheader' => $singleHeader, 'tag' => $tag, 'helperPath' => $helperPath, 'direction' => $direction, 'onchange' => $onchange, 'minYear' => $minYear, 'maxYear' => $maxYear, 'dataAttribute' => '', 'dataAttributes' => '', 'calendar' => $calendar, 'firstday' => $lang->getFirstDay(), 'weekend' => explode(',', $lang->getWeekEnd()));
    return LayoutHelper::render('joomla.form.field.calendar', $data, null, null);
}