示例#1
0
 /**
  * Use a numeric timestamp to create a new Date object
  *
  * @param $timestamp mixed Timestamp used to create Date object
  *
  * @return Date New Date instance
  */
 public static function createFromTimestamp($timestamp)
 {
     $date = new Date();
     $date->setRawString($timestamp);
     $date->setTimestamp($timestamp);
     return $date;
 }
示例#2
0
 private function parse($dateString, $time = null)
 {
     $retval = array($dateString, array());
     $date = $dateString;
     if (is_numeric($date)) {
         try {
             $date = Date::createFromTimestamp($dateString);
         } catch (Exception $e) {
             // Unable to create DateTime object with dateString
             return $retval;
         }
     }
     if (!$date instanceof DateTime) {
         $info = date_parse($dateString);
         // If no month or day is parsed, assume this is an invalid format
         if (!$info['month'] || !$info['day']) {
             return null;
         }
         if ($info['errors']) {
             $keys = array_keys($info['errors']);
             $offset = array_shift($keys);
             $date = substr($dateString, 0, $offset);
             if (!$time) {
                 $time = substr($dateString, $offset + 1);
             }
             $info = date_parse($date);
             if ($info['errors']) {
                 return $retval;
             }
             try {
                 $date = new Date($date);
             } catch (Exception $e) {
                 // Unable to create DateTime object
                 return $retval;
             }
         } else {
             if (!$info['year']) {
                 $info['year'] = date('Y');
             }
             try {
                 $date = new Date("{$info['month']}/{$info['day']}/{$info['year']}");
             } catch (Exception $e) {
                 // Unable to create DateTime object
                 return $retval;
             }
             if (!$time) {
                 if ($info['hour'] === 0 && $info['minute'] === 0) {
                     $time = null;
                 } else {
                     $time = "{$info['hour']}:{$info['minute']}";
                 }
             }
         }
     } else {
         if (!$time) {
             if ($date->format("g:ia") !== "12:00am") {
                 $time = $date->format("g:ia");
             }
         }
     }
     $instances = array();
     if ($this->_parseTimes) {
         $time = preg_replace(array('#\\s+(?:(?:un)?\'?til{1,2}|thr(?:ough|u))\\s+#i', '#\\s(a|p)\\.?m\\.?#i', '#(\\s|-)(noon)(\\s|-|,|$)#i', '#(\\s|-)(midnight)(\\s|-|,|$)#i'), array('-', '$1m', '${1}12:00pm$3', '${1}12:00am$3'), $time);
         if (preg_match_all('#((?:.|\\s)*?)(\\d{1,2}(?:\\:\\d{2})?\\s*(?:(?:a|p)m)?(?:-|$|,|\\s+and\\s+|\\s+))#i', $time, $matches)) {
             $skip = 0;
             foreach ($matches[2] as $index => $match) {
                 if ($skip > 0) {
                     $skip--;
                     continue;
                 }
                 $instance = new Instance($time);
                 if (preg_match('#\\A(.+?),\\s*$#', $match, $found)) {
                     $instance->setStartTime(new Time($found[1]));
                 } else {
                     if (preg_match('#\\A(.+?)-$#', $match, $found)) {
                         $skip++;
                         $instance->setStartTime(new Time($found[1]));
                         if (isset($matches[0][$index + 1])) {
                             // Sometimes the time is formatted '6:30pm-until'.
                             // If no end time, only generate start time.
                             $instance->setEndTime(new Time($matches[0][$index + 1]));
                         }
                     } else {
                         $instance->setStartTime(new Time($match));
                     }
                 }
                 if (isset($matches[1][$index])) {
                     /* @var $instance Instance */
                     $instance->setStartDescription($matches[1][$index]);
                     if (isset($matches[1][$index + 1])) {
                         if ($instance->getEndTime()) {
                             $instance->setEndDescription($matches[1][$index + 1]);
                         }
                     }
                 }
                 $instances[] = $instance;
             }
         } else {
             // throw error?
         }
     } else {
         $instances[] = new Instance($time);
     }
     return array($date, $instances);
 }