コード例 #1
0
ファイル: datetime.php プロジェクト: habari/system
 /**
  * Helper function to create a DateTime object for the given
  * time and timezone. If no time is given, defaults to 'now'. If no
  * timezone given defaults to timezone set in {@link set_default_timezone()}
  *
  * @static
  * @see DateTime::__construct()
  * @param string $time String in a format accepted by
  * {@link http://ca.php.net/strtotime strtotime()}, defaults to "now".
  * @param string $timezone A timezone name, not an abbreviation.
  * @return \Habari\DateTime
  */
 public static function create($time = null, $timezone = null)
 {
     if ($time instanceof DateTime) {
         return $time;
     } elseif ($time instanceof \DateTime) {
         $time = $time->format('U');
     } elseif ($time === null) {
         $time = 'now';
     } elseif (is_numeric($time)) {
         $time = '@' . $time;
     }
     if ($timezone === null) {
         $timezone = self::$default_timezone;
     }
     try {
         $datetime = new DateTime($time);
         // passing the timezone to construct doesn't seem to do anything.
         $datetime->set_timezone($timezone);
     } catch (\Exception $e) {
         // Do not crash if invalid data has been passed but save the exception
         self::$last_exception = $e;
         return false;
     }
     return $datetime;
 }