Horde_Date::__construct PHP Method

__construct() public method

Recognized formats: - arrays with keys 'year', 'month', 'mday', 'day' 'hour', 'min', 'minute', 'sec' - objects with properties 'year', 'month', 'mday', 'hour', 'min', 'sec' - yyyy-mm-dd hh:mm:ss - yyyymmddhhmmss - yyyymmddThhmmssZ - yyyymmdd (might conflict with unix timestamps between 31 Oct 1966 and 03 Mar 1973) - unix timestamps - anything parsed by strtotime()/DateTime.
public __construct ( $date = null, $timezone = null )
    public function __construct($date = null, $timezone = null)
    {
        if (!self::$_supportedSpecs) {
            self::$_supportedSpecs = self::$_defaultSpecs;
            if (function_exists('nl_langinfo')) {
                self::$_supportedSpecs .= 'bBpxX';
            }
        }
        if (func_num_args() > 2) {
            // Handle args in order: year month day hour min sec tz
            $this->_initializeFromArgs(func_get_args());
            return;
        }
        $this->_initializeTimezone($timezone);
        if (is_null($date)) {
            return;
        }
        if (is_string($date)) {
            $date = trim($date, '"');
        }
        if (is_object($date)) {
            $this->_initializeFromObject($date);
        } elseif (is_array($date)) {
            $this->_initializeFromArray($date);
        } elseif (preg_match('/^(\\d{4})-?(\\d{2})-?(\\d{2})T? ?(\\d{2}):?(\\d{2}):?(\\d{2})(?:\\.\\d+)?(Z?)$/', $date, $parts)) {
            $this->_year = (int) $parts[1];
            $this->_month = (int) $parts[2];
            $this->_mday = (int) $parts[3];
            $this->_hour = (int) $parts[4];
            $this->_min = (int) $parts[5];
            $this->_sec = (int) $parts[6];
            if ($parts[7]) {
                $this->_initializeTimezone('UTC');
            }
        } elseif (preg_match('/^(\\d{4})-?(\\d{2})-?(\\d{2})$/', $date, $parts) && $parts[2] > 0 && $parts[2] <= 12 && $parts[3] > 0 && $parts[3] <= 31) {
            $this->_year = (int) $parts[1];
            $this->_month = (int) $parts[2];
            $this->_mday = (int) $parts[3];
            $this->_hour = $this->_min = $this->_sec = 0;
        } elseif ((string) (int) $date == $date) {
            // Try as a timestamp.
            if ($this->_timezone) {
                $oldtimezone = date_default_timezone_get();
                date_default_timezone_set($this->_timezone);
            }
            $parts = @getdate($date);
            if ($parts) {
                $this->_year = $parts['year'];
                $this->_month = $parts['mon'];
                $this->_mday = $parts['mday'];
                $this->_hour = $parts['hours'];
                $this->_min = $parts['minutes'];
                $this->_sec = $parts['seconds'];
            }
            if ($this->_timezone) {
                date_default_timezone_set($oldtimezone);
            }
        } else {
            if (!empty($timezone)) {
                try {
                    $parsed = new DateTime($date, new DateTimeZone($timezone));
                } catch (Exception $e) {
                    throw new Horde_Date_Exception(sprintf(Horde_Date_Translation::t("Failed to parse time string (%s)"), $date));
                }
            } else {
                try {
                    $parsed = new DateTime($date);
                } catch (Exception $e) {
                    throw new Horde_Date_Exception(sprintf(Horde_Date_Translation::t("Failed to parse time string (%s)"), $date));
                }
                $parsed->setTimezone(new DateTimeZone(date_default_timezone_get()));
                $this->_initializeTimezone(date_default_timezone_get());
            }
            $this->_year = (int) $parsed->format('Y');
            $this->_month = (int) $parsed->format('m');
            $this->_mday = (int) $parsed->format('d');
            $this->_hour = (int) $parsed->format('H');
            $this->_min = (int) $parsed->format('i');
            $this->_sec = (int) $parsed->format('s');
        }
    }

Usage Example

Ejemplo n.º 1
0
Archivo: Day.php Proyecto: horde/horde
 /**
  * Constructor.
  *
  * @param integer $month
  * @param integer $day
  * @param integer $year
  */
 public function __construct($month = null, $day = null, $year = null)
 {
     if (is_null($month)) {
         $month = date('n');
     }
     if (is_null($year)) {
         $year = date('Y');
     }
     if (is_null($day)) {
         $day = date('j');
     }
     parent::__construct(array('year' => $year, 'month' => $month, 'mday' => $day));
     $this->slotsPerHour = $GLOBALS['prefs']->getValue('slots_per_hour');
     if (!$this->slotsPerHour) {
         $this->slotsPerHour = 1;
     }
     $this->slotsPerDay = $this->slotsPerHour * 24;
     $this->slotLength = 60 / $this->slotsPerHour;
     for ($i = 0; $i < $this->slotsPerDay; $i++) {
         $minutes = $i * $this->slotLength;
         $this->slots[$i]['hour'] = (int) ($minutes / 60);
         $this->slots[$i]['min'] = $minutes % 60;
     }
 }