예제 #1
0
 /**
  * Deconstructs the passed date value into all time units
  *
  * @param string|int|array|DateTime $value Value to deconstruct.
  * @param array $options Options for conversion.
  * @return array
  */
 protected function _deconstructDate($value, $options)
 {
     if (empty($value)) {
         return ['year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => '', 'meridian' => ''];
     }
     try {
         if (is_string($value)) {
             $date = new \DateTime($value);
         } elseif (is_bool($value) || $value === null) {
             $date = new \DateTime();
         } elseif (is_int($value)) {
             $date = new \DateTime('@' . $value);
         } elseif (is_array($value)) {
             $date = new \DateTime();
             if (isset($value['year'], $value['month'], $value['day'])) {
                 $date->setDate($value['year'], $value['month'], $value['day']);
             }
             if (!isset($value['second'])) {
                 $value['second'] = 0;
             }
             if (isset($value['meridian'])) {
                 $isAm = strtolower($value['meridian']) === 'am';
                 $value['hour'] = $isAm ? $value['hour'] : $value['hour'] + 12;
             }
             if (isset($value['hour'], $value['minute'], $value['second'])) {
                 $date->setTime($value['hour'], $value['minute'], $value['second']);
             }
         } else {
             $date = clone $value;
         }
     } catch (\Exception $e) {
         $date = new \DateTime();
     }
     if (isset($options['minute']['interval'])) {
         $change = $this->_adjustValue($date->format('i'), $options['minute']);
         $date->modify($change > 0 ? "+{$change} minutes" : "{$change} minutes");
     }
     return ['year' => $date->format('Y'), 'month' => $date->format('m'), 'day' => $date->format('d'), 'hour' => $date->format('H'), 'minute' => $date->format('i'), 'second' => $date->format('s'), 'meridian' => $date->format('a')];
 }
예제 #2
0
 /**
  * Deconstructs the passed date value into all time units
  *
  * @param string|int|array|\DateTime|null $value Value to deconstruct.
  * @param array $options Options for conversion.
  * @return array
  */
 protected function _deconstructDate($value, $options)
 {
     if ($value === '' || $value === null) {
         return ['year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => '', 'meridian' => ''];
     }
     try {
         if (is_string($value)) {
             $date = new \DateTime($value);
         } elseif (is_bool($value)) {
             $date = new \DateTime();
         } elseif (is_int($value)) {
             $date = new \DateTime('@' . $value);
         } elseif (is_array($value)) {
             $dateArray = ['year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => '', 'meridian' => 'pm'];
             $validDate = false;
             foreach ($dateArray as $key => $dateValue) {
                 $exists = isset($value[$key]);
                 if ($exists) {
                     $validDate = true;
                 }
                 if ($exists && $value[$key] !== '') {
                     $dateArray[$key] = str_pad($value[$key], 2, '0', STR_PAD_LEFT);
                 }
             }
             if ($validDate) {
                 if (!isset($dateArray['second'])) {
                     $dateArray['second'] = 0;
                 }
                 if (isset($value['meridian'])) {
                     $isAm = strtolower($dateArray['meridian']) === 'am';
                     $dateArray['hour'] = $isAm ? $dateArray['hour'] : $dateArray['hour'] + 12;
                 }
                 if (!empty($dateArray['minute']) && isset($options['minute']['interval'])) {
                     $dateArray['minute'] += $this->_adjustValue($dateArray['minute'], $options['minute']);
                 }
                 return $dateArray;
             }
             $date = new \DateTime();
         } else {
             $date = clone $value;
         }
     } catch (\Exception $e) {
         $date = new \DateTime();
     }
     if (isset($options['minute']['interval'])) {
         $change = $this->_adjustValue($date->format('i'), $options['minute']);
         $date->modify($change > 0 ? "+{$change} minutes" : "{$change} minutes");
     }
     return ['year' => $date->format('Y'), 'month' => $date->format('m'), 'day' => $date->format('d'), 'hour' => $date->format('H'), 'minute' => $date->format('i'), 'second' => $date->format('s'), 'meridian' => $date->format('a')];
 }
예제 #3
0
 /**
  * Test rendering the default year widget.
  *
  * @return void
  */
 public function testRenderYearWidgetDefaultRange()
 {
     $now = new \DateTime();
     $result = $this->DateTime->render(['month' => false, 'day' => false, 'hour' => false, 'minute' => false, 'second' => false, 'val' => $now]);
     $year = $now->format('Y');
     $format = '<option value="%s" selected="selected">%s</option>';
     $this->assertContains(sprintf($format, $year, $year), $result);
     $format = '<option value="%s">%s</option>';
     $maxYear = $now->format('Y') + 5;
     $minYear = $now->format('Y') - 5;
     $this->assertContains(sprintf($format, $maxYear, $maxYear), $result);
     $this->assertContains(sprintf($format, $minYear, $minYear), $result);
     $nope = $now->format('Y') + 6;
     $this->assertNotContains(sprintf($format, $nope, $nope), $result);
     $nope = $now->format('Y') - 6;
     $this->assertNotContains(sprintf($format, $nope, $nope), $result);
 }