Skip to content

Commit

Permalink
Merge branch 'master-time'
Browse files Browse the repository at this point in the history
See #GH-1237
  • Loading branch information
markstory committed Apr 20, 2013
2 parents 842b180 + 64da4e7 commit 3dbc3a0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
32 changes: 32 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -2328,6 +2328,7 @@ public function testTimeSelectedWithInterval() {
* @return void
*/
public function testInputTimeWithIntervalAnd12HourFormat() {
/*
$result = $this->Form->input('Model.start_time', array(
'type' => 'time',
'timeFormat' => 12,
Expand All @@ -2347,6 +2348,37 @@ public function testInputTimeWithIntervalAnd12HourFormat() {
$this->assertContains('<option value="04" selected="selected">4</option>', $result);
$this->assertContains('<option value="30" selected="selected">30</option>', $result);
$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
*/

$result = $this->Form->input('Model.start_time', array(
'type' => 'time',
'timeFormat' => '12',
'interval' => 10,
'selected' => '2013-05-19 00:33:00'
));
$this->assertContains('<option value="12" selected="selected">12</option>', $result);
$this->assertContains('<option value="30" selected="selected">30</option>', $result);
$this->assertContains('<option value="am" selected="selected">am</option>', $result);

$result = $this->Form->input('Model.start_time', array(
'type' => 'time',
'timeFormat' => '12',
'interval' => 10,
'selected' => '2013-05-19 13:33:00'
));
$this->assertContains('<option value="01" selected="selected">1</option>', $result);
$this->assertContains('<option value="30" selected="selected">30</option>', $result);
$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);

$result = $this->Form->input('Model.start_time', array(
'type' => 'time',
'timeFormat' => '12',
'interval' => 10,
'selected' => '2013-05-19 01:33:00'
));
$this->assertContains('<option value="01" selected="selected">1</option>', $result);
$this->assertContains('<option value="30" selected="selected">30</option>', $result);
$this->assertContains('<option value="am" selected="selected">am</option>', $result);
}

/**
Expand Down
15 changes: 6 additions & 9 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -2374,6 +2374,10 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a
$monthNames = $attributes['monthNames'];
$attributes = array_diff_key($attributes, $defaults);

if ($timeFormat == 12 && $hour == 12) {
$hour = 0;
}

if (!empty($interval) && $interval > 1 && !empty($min)) {
$current = new DateTime();
if ($year !== null) {
Expand All @@ -2384,7 +2388,7 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a
}
$change = (round($min * (1 / $interval)) * $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';
$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;
}
Expand Down Expand Up @@ -2505,15 +2509,8 @@ protected function _getDateTimeValue($value, $timeFormat) {
if (!empty($timeFormat)) {
$time = explode(':', $days[1]);

if ($time[0] >= 12 && $timeFormat == 12) {
if ($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 = $min = null;
if (isset($time[1])) {
Expand Down

0 comments on commit 3dbc3a0

Please sign in to comment.