CakePHP 2.2.3 FormHelper cannot set default meridian when using minute interval and default minute value

StackOverflow https://stackoverflow.com/questions/15390026

  •  23-03-2022
  •  | 
  •  

문제

In my application, the following is expected to output the hour, minute, and meridian <select>s for a 12-hour time with 4:30pm selected by default:

echo $this->Form->input('time_example', array(
    'interval' => 5,
    'timeFormat' => '12',
    'type' => 'time',
    'selected' => array(
        'hour' => '4',
        'min' => '30',
        'meridian' => 'pm'
    )
));

But the default selected time is actually displayed as 4:30am.

After some fiddling, I found that it will correctly default to 4:30pm if interval is removed from the options, and it will correctly default to 4:00pm if min is removed from the options.

I dug into FormHelper.php and found this in FormHelper::dateTime(), starting at line 2246 (in CakePHP version 2.2.3):

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);
    }
    $change = (round($min * (1 / $interval)) * $interval) - $min;
    $current->modify($change > 0 ? "+$change minutes" : "$change minutes");
    $newTime = explode(' ', $current->format('Y m d H i a'));
    list($year, $month, $day, $hour, $min, $meridian) = $newTime;
}

This appears that if both $interval and $min are set, this prevents $min from conflicting with $interval, but $meridian gets overwritten with the assumption that $hour is in a 24-hour format. This forces $meridian to become 'am' for any value of $hour less than 12, making a default selection of 4:30pm impossible.

Am I using the helper incorrectly, or is this an error in the core?

도움이 되었습니까?

해결책

This bug has been corrected in this merge.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top