Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

provide ability for rounding - closes #1986

  • Loading branch information...
commit b412e2cbb19c4c93b5f8e86a177889ad1f59bcea 1 parent 532b209
@dereuromark dereuromark authored
View
27 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
@@ -5878,6 +5878,33 @@ public function testDateTime() {
}
/**
+ * Test dateTime with rounding
+ *
+ * @return void
+ */
+ public function testDateTimeRounding() {
+ $this->Form->request->data['Contact'] = array(
+ 'date' => array(
+ 'day' => '13',
+ 'month' => '12',
+ 'year' => '2010',
+ 'hour' => '04',
+ 'min' => '19',
+ 'meridian' => 'AM'
+ )
+ );
+
+ $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
+ $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
+
+ $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
+ $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
+
+ $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
+ $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
+ }
+
+/**
* Test that empty values don't trigger errors.
*
* @return void
View
17 lib/Cake/View/Helper/FormHelper.php
@@ -2345,6 +2345,7 @@ public function meridian($fieldName, $attributes = array()) {
* - `separator` The contents of the string between select elements. Defaults to '-'
* - `empty` - If true, the empty select option is shown. If a string,
* that string is displayed as the empty element.
+ * - `round` - Set to `up` or `down` if you want to force rounding in either direction. Defaults to null.
* - `value` | `default` The default value to be used by the input. A value in `$this->data`
* matching the field name will override this value. If no default is provided `time()` will be used.
*
@@ -2379,7 +2380,7 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a
$defaults = array(
'minYear' => null, 'maxYear' => null, 'separator' => '-',
- 'interval' => 1, 'monthNames' => true
+ 'interval' => 1, 'monthNames' => true, 'round' => null
);
$attributes = array_merge($defaults, (array)$attributes);
if (isset($attributes['minuteInterval'])) {
@@ -2391,6 +2392,7 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a
$separator = $attributes['separator'];
$interval = $attributes['interval'];
$monthNames = $attributes['monthNames'];
+ $round = $attributes['round'];
$attributes = array_diff_key($attributes, $defaults);
if ($timeFormat == 12 && $hour == 12) {
@@ -2405,7 +2407,18 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a
if ($hour !== null) {
$current->setTime($hour, $min);
}
- $change = (round($min * (1 / $interval)) * $interval) - $min;
+ $changeValue = $min * (1 / $interval);
+ switch ($round) {
+ case 'up':
+ $changeValue = ceil($changeValue);
+ break;
+ case 'down':
+ $changeValue = floor($changeValue);
+ break;
+ default:
+ $changeValue = round($changeValue);
+ }
+ $change = ($changeValue * $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';
$newTime = explode(' ', $current->format($format));
Please sign in to comment.
Something went wrong with that request. Please try again.