Skip to content

Commit

Permalink
Make minutes/hours wrap around correctly.
Browse files Browse the repository at this point in the history
Also account for times around midnight that roll over days/months/years.

Fixes #3242
  • Loading branch information
markstory committed Sep 29, 2012
1 parent 853d866 commit 80c355b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
37 changes: 37 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -2229,6 +2229,43 @@ public function testInputTime() {
$this->assertRegExp('#<option value="pm"[^>]*>pm</option>#', $result);
}

/**
* Test interval + selected near the hour roll over.
*
* @return void
*/
public function testTimeSelectedWithInterval() {
$result = $this->Form->input('Model.start_time', array(
'timeFormat' => 24,
'type' => 'time',
'interval' => 15,
'selected' => '15:57'
));
$this->assertContains('<option value="16" selected="selected">16</option>', $result);
$this->assertContains('<option value="00" selected="selected">00</option>', $result);

$result = $this->Form->input('Model.start_time', array(
'timeFormat' => 24,
'type' => 'time',
'interval' => 15,
'selected' => '23:57'
));
$this->assertContains('<option value="00" selected="selected">0</option>', $result);
$this->assertContains('<option value="00" selected="selected">00</option>', $result);

$result = $this->Form->input('Model.created', array(
'timeFormat' => 24,
'type' => 'datetime',
'interval' => 15,
'selected' => '2012-09-30 23:56'
));
$this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
$this->assertContains('<option value="10" selected="selected">October</option>', $result);
$this->assertContains('<option value="01" selected="selected">1</option>', $result);
$this->assertContains('<option value="00" selected="selected">0</option>', $result);
$this->assertContains('<option value="00" selected="selected">00</option>', $result);
}

/**
* test form->input() with datetime, date and time types
*
Expand Down
17 changes: 14 additions & 3 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -2244,6 +2244,20 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a
$monthNames = $attributes['monthNames'];
$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);
}
$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;
}

if (isset($attributes['id'])) {
if (is_string($attributes['id'])) {
// build out an array version
Expand Down Expand Up @@ -2305,9 +2319,6 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a
}
$opt = implode($separator, $selects);

if (!empty($interval) && $interval > 1 && !empty($min)) {
$min = round($min * (1 / $interval)) * $interval;
}
$selectMinuteAttr['interval'] = $interval;
switch ($timeFormat) {
case '24':
Expand Down

0 comments on commit 80c355b

Please sign in to comment.