BcFreezeHelper::dateTime PHP Method

dateTime() public method

日付タグを表示
public dateTime ( string $fieldName, string $dateFormat = 'DMY', string $timeFormat = '12', array $attributes = [] ) : string
$fieldName string フィールド文字列
$dateFormat string 日付フォーマット
$timeFormat string 時間フォーマット
$attributes array html属性 - 凍結時、$attributes['selected']に要素を格納することで日付を選択する (例) $attributes['selected'] = array('selected' => array('year' => '2010', 'month' => '4', 'day' => '1'))
return string htmlタグ
    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) . "日";
            if ($timeFormat) {
                $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);
        }
    }