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.