public dateTime ( string $fieldName, string $dateFormat = 'DMY', string $timeFormat = '12', array $attributes = [] ) : string | ||
$fieldName | string | Prefix name for the SELECT element |
$dateFormat | string | DMY, MDY, YMD, or null to not generate date inputs. - W が入力された場合、和暦のselectと年月日の接尾辞が付与される |
$timeFormat | string | 12, 24, or null to not generate time inputs. |
$attributes | array | Array of Attributes |
return | string | Generated set of select boxes for the date and time formats chosen. |
public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $attributes = array())
{
$attributes += array('empty' => true, 'value' => null);
$year = $month = $day = $hour = $min = $meridian = null;
if (empty($attributes['value'])) {
$attributes = $this->value($attributes, $fieldName);
}
if ($attributes['value'] === null && $attributes['empty'] != true) {
$attributes['value'] = time();
if (!empty($attributes['maxYear']) && $attributes['maxYear'] < date('Y')) {
$attributes['value'] = strtotime(date($attributes['maxYear'] . '-m-d'));
}
}
if (!empty($attributes['value'])) {
list($year, $month, $day, $hour, $min, $meridian) = $this->_getDateTimeValue($attributes['value'], $timeFormat);
}
// >>> CUSTOMIZE MODIFY 2011/01/11 ryuring 日本対応
/* $defaults = array(
'minYear' => null, 'maxYear' => null, 'separator' => '-',
'interval' => 1, 'monthNames' => true, 'round' => null
); */
// ---
$defaults = array('minYear' => null, 'maxYear' => null, 'separator' => ' ', 'interval' => 1, 'monthNames' => '', 'round' => null);
// <<<
$attributes = array_merge($defaults, (array) $attributes);
if (isset($attributes['minuteInterval'])) {
$attributes['interval'] = $attributes['minuteInterval'];
unset($attributes['minuteInterval']);
}
$minYear = $attributes['minYear'];
$maxYear = $attributes['maxYear'];
$separator = $attributes['separator'];
$interval = $attributes['interval'];
$monthNames = $attributes['monthNames'];
$round = $attributes['round'];
$attributes = array_diff_key($attributes, $defaults);
if (!empty($interval) && $interval > 1 && !empty($min)) {
$current = new DateTime();
if ($year !== null) {
$current->setDate($year, $month, $day);
}
if ($hour !== null) {
$current->setTime($hour, $min);
}
$changeValue = $min * (1 / $interval);
switch ($round) {
case 'up':
$changeValue = ceil($changeValue);
break;
case 'down':
$changeValue = floor($changeValue);
break;
default:
$changeValue = round($changeValue);
}
$change = $changeValue * $interval - $min;
$current->modify($change > 0 ? "+{$change} minutes" : "{$change} minutes");
$format = $timeFormat == 12 ? 'Y m d h i a' : 'Y m d H i a';
$newTime = explode(' ', $current->format($format));
list($year, $month, $day, $hour, $min, $meridian) = $newTime;
}
$keys = array('Day', 'Month', 'Year', 'Hour', 'Minute', 'Meridian');
$attrs = array_fill_keys($keys, $attributes);
$hasId = isset($attributes['id']);
if ($hasId && is_array($attributes['id'])) {
// check for missing ones and build selectAttr for each element
$attributes['id'] += array('month' => '', 'year' => '', 'day' => '', 'hour' => '', 'minute' => '', 'meridian' => '');
foreach ($keys as $key) {
$attrs[$key]['id'] = $attributes['id'][strtolower($key)];
}
}
if ($hasId && is_string($attributes['id'])) {
// build out an array version
foreach ($keys as $key) {
$attrs[$key]['id'] = $attributes['id'] . $key;
}
}
if (is_array($attributes['empty'])) {
$attributes['empty'] += array('month' => true, 'year' => true, 'day' => true, 'hour' => true, 'minute' => true, 'meridian' => true);
foreach ($keys as $key) {
$attrs[$key]['empty'] = $attributes['empty'][strtolower($key)];
}
}
$selects = array();
foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) {
switch ($char) {
// >>> CUSTOMIZE ADD 2011/01/11 ryuring 和暦対応
case 'W':
$selects[] = $this->wyear($fieldName, $minYear, $maxYear, $year, $attributes, $attributes['empty']) . "年";
break;
// <<<
// <<<
case 'Y':
$attrs['Year']['value'] = $year;
// >>> CUSTOMIZE MODIFY 2011/01/11 ryuring 日本対応
/* $selects[] = $this->year(
$fieldName, $minYear, $maxYear, $attrs['Year']
); */
// ---
$suffix = preg_match('/^W/', $dateFormat) ? '年' : '';
$selects[] = $this->year($fieldName, $minYear, $maxYear, $attrs['Year']) . $suffix;
// <<<
break;
case 'M':
$attrs['Month']['value'] = $month;
$attrs['Month']['monthNames'] = $monthNames;
// >>> CUSTOMIZE MODIFY 2011/01/11 ryuring 日本対応
/* $selects[] = $this->month($fieldName, $attrs['Month']); */
// ---
$suffix = preg_match('/^W/', $dateFormat) ? '月' : '';
$selects[] = $this->month($fieldName, $attrs['Month']) . $suffix;
// <<<
break;
case 'D':
$attrs['Day']['value'] = $day;
// >>> CUSTOMIZE MODIFY 2011/01/11 ryuring 日本対応
/* $selects[] = $this->day($fieldName, $attrs['Day']); */
// ---
$suffix = preg_match('/^W/', $dateFormat) ? '日' : '';
$selects[] = $this->day($fieldName, $attrs['Day']) . $suffix;
// <<<
break;
}
}
$opt = implode($separator, $selects);
$attrs['Minute']['interval'] = $interval;
switch ($timeFormat) {
case '24':
$attrs['Hour']['value'] = $hour;
$attrs['Minute']['value'] = $min;
$opt .= $this->hour($fieldName, true, $attrs['Hour']) . ':' . $this->minute($fieldName, $attrs['Minute']);
break;
case '12':
$attrs['Hour']['value'] = $hour;
$attrs['Minute']['value'] = $min;
$attrs['Meridian']['value'] = $meridian;
$opt .= $this->hour($fieldName, false, $attrs['Hour']) . ':' . $this->minute($fieldName, $attrs['Minute']) . ' ' . $this->meridian($fieldName, $attrs['Meridian']);
break;
}
return $opt;
}
/** * 日付タグを表示 * * @param string $fieldName フィールド文字列 * @param string $dateFormat 日付フォーマット * @param string $timeFormat 時間フォーマット * @param array $attributes html属性 * - 凍結時、$attributes['selected']に要素を格納することで日付を選択する * (例) $attributes['selected'] = array('selected' => array('year' => '2010', 'month' => '4', 'day' => '1')) * @return string htmlタグ * @access public */ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $attributes = array()) { if ($this->freezed) { $year = $month = $day = $hour = $min = $meridian = $showEmpty = $selected = null; if (isset($attributes['selected'])) { $selected = $attributes['selected']; } if (isset($attributes['empty'])) { $showEmpty = $attributes['empty']; } if (empty($selected)) { $selected = $this->value($fieldName); } if ($selected === null && $showEmpty != true) { $selected = time(); } if (!empty($selected)) { if (is_array($selected)) { extract($selected); } else { if (is_numeric($selected)) { $selected = strftime('%Y-%m-%d %H:%M:%S', $selected); } $meridian = 'am'; $pos = strpos($selected, '-'); if ($pos !== false) { $date = explode('-', $selected); $days = explode(' ', $date[2]); $day = $days[0]; $month = $date[1]; $year = $date[0]; } else { $days[1] = $selected; } if ($timeFormat != 'NONE' && !empty($timeFormat)) { $time = explode(':', $days[1]); $check = str_replace(':', '', $days[1]); if ($check > 115959 && $timeFormat == '12') { $time[0] = $time[0] - 12; $meridian = 'pm'; } elseif ($time[0] == '00' && $timeFormat == '12') { $time[0] = 12; } elseif ($time[0] > 12) { $meridian = 'pm'; } if ($time[0] == 0 && $timeFormat == '12') { $time[0] = 12; } $hour = $time[0]; $min = $time[1]; } } } $elements = array('Day', 'Month', 'Year', 'Hour', 'Minute', 'Meridian'); $defaults = array('minYear' => null, 'maxYear' => null, 'separator' => ' '); $attributes = array_merge($defaults, (array) $attributes); $minYear = $attributes['minYear']; $maxYear = $attributes['maxYear']; $separator = $attributes['separator']; if (isset($attributes['id'])) { if (is_string($attributes['id'])) { // build out an array version foreach ($elements as $element) { $selectAttrName = 'select' . $element . 'Attr'; ${$selectAttrName} = $attributes; ${$selectAttrName}['id'] = $attributes['id'] . $element; } } elseif (is_array($attributes['id'])) { // check for missing ones and build selectAttr for each element foreach ($elements as $element) { $selectAttrName = 'select' . $element . 'Attr'; ${$selectAttrName} = $attributes; ${$selectAttrName}['id'] = $attributes['id'][strtolower($element)]; } } } else { // build the selectAttrName with empty id's to pass foreach ($elements as $element) { $selectAttrName = 'select' . $element . 'Attr'; ${$selectAttrName} = $attributes; } } $selects = array(); if (preg_match('/^W/', $dateFormat)) { $selects[] = $this->wyear($fieldName, $minYear, $maxYear, $year, $selectYearAttr, $showEmpty) . "年"; } else { $selectYearAttr['value'] = $year; $selects[] = $this->freezeControll($fieldName . ".year", array(), $selectYearAttr) . "年"; } // TODO 値の出力はBcTextにまとめた方がよいかも // メール本文への出力も同じ処理を利用する。(改行の処理などはどうするか。。) $selectMonthAttr['value'] = $month; $selectDayAttr['value'] = $day; $selects[] = $this->freezeControll($fieldName . ".month", array(), $selectMonthAttr) . "月"; $selects[] = $this->freezeControll($fieldName . ".day", array(), $selectDayAttr) . "日"; $selects[] = $this->freezeControll($fieldName . ".hour", array(), array('value' => $hour)) . "時"; $selects[] = $this->freezeControll($fieldName . ".min", array(), array('value' => $min)) . "分"; return implode($separator, $selects); } else { return parent::dateTime($fieldName, $dateFormat, $timeFormat, $attributes); } }