Location: PHPKode > scripts > DATA > data/DATA/SQLTime.php
<?php
/**
 * @package DATA
 */

/**
 * ANSI SQL Time data type representation.
 * 
 * When inboxing, if the time is invalid,
 * {@link DATA_InvalidTime} is thrown.
 * 
 * @todo Time math.
 */
class DATA_SQLTime extends DATA_SQLType {
    /**
     * The time hour.
     * @var int
     */
    protected $hour;
    /**
     * The time minutes.
     * @var int
     */
    protected $minutes;
    /**
     * The time seconds.
     * @var int
     */
    protected $seconds;
    
    /**
     * Builds a Date data type with the specified year, month and day, or todays date if not specified.
     * 
     * Throws {@link DATA_InvalidTime}.
     * 
     * @param boolean $nullable True if the type is nullable.
     * @param int $hour The time hour.
     * @param int $minutes The time minutes.
     * @param int $seconds The time seconds.
     */
    public function __construct($nullable, $hour = null, $minutes = null, $seconds = null) {
        parent::__construct($nullable, is_null($hour) || is_null($minutes) || is_null($seconds));
        if (!$this->isNull()) {
            $this->setTime($hour, $minutes, $seconds);
        }
    }
    
    /**
     * Returns a time object with now's time.
     * 
     * @return DATA_SQLTime Now's time.
     */
    public static function now() {
        $now = time();
        $hour = (int)date("G", $now);
        $minutes = (int)date("i", $now);
        $seconds = (int)date("s", $now);
        return new DATA_SQLTime(true, $hour, $minutes, $seconds);
    }
    
    /**
     * Returns the time hour.
     * 
     * @return int The time hour.
     */
    public function getHour() {
        return $this->hour;
    }
    
    /**
     * Sets the time hour.
     * 
     * Throws {@link DATA_InvalidTime}.
     * 
     * @param int $year The time hour.
     */
    public function setHour($hour) {
        $this->setTime($hour, $this->minutes, $this->seconds);
    }
    
    /**
     * Returns the time minutes.
     * 
     * @return int The time minutes.
     */
    public function getMinutes() {
        return $this->month;
    }
    
    /**
     * Sets the time minutes.
     * 
     * Throws {@link DATA_InvalidTime}.
     * 
     * @param int $year The time minutes.
     */
    public function setMinutes($minutes) {
        $this->setTime($this->hour, $minutes, $this->seconds);
    }
    
    /**
     * Returns the time seconds.
     * 
     * @return int The time seconds.
     */
    public function getSeconds() {
        return $this->day;
    }
    
    /**
     * Sets the time seconds.
     * 
     * Throws {@link DATA_InvalidTime}.
     * 
     * @param int $year The time seconds.
     */
    public function setSeconds($seconds) {
        $this->setTime($this->hour, $this->minutes, $seconds);
    }
    
    /**
     * Sets all the time values (hour, minutes, seconds).
     * 
     * Throws {@link DATA_InvalidTime}.
     * 
     * @param int $hour The time hour.
     * @param int $minutes The time minutes.
     * @param int $seconds The time seconds.
     */
    public function setTime($hour, $minutes, $seconds) {
        if (!$this->checkTime($hour, $minutes, $seconds)) {
            throw new DATA_InvalidTime("$hour:$minutes:$seconds");
        }
        $this->hour = $hour;
        $this->minutes = $minutes;
        $this->seconds = $seconds;
        $this->setNotNull();
    }
    
    public function setNull() {
        parent::setNull();
        $this->hour = null;
        $this->minutes = null;
        $this->seconds = null;
    }
    
    public function __toString() {
        return str_pad($this->hour, 2, '0', STR_PAD_LEFT) . ':'
             . str_pad($this->minutes, 2, '0', STR_PAD_LEFT) . ':'
             . str_pad($this->seconds, 2, '0', STR_PAD_LEFT);
    }
    
    /**
     * Checks if a given time is valid.
     * 
     * @param int $hour The time hour.
     * @param int $minutes The time minutes.
     * @param int $seconds The time seconds.
     * @return bool True if the time is valid.
     */
    protected function checkTime($hour, $minutes, $seconds) {
        if ($hour >= 24 || $hour < 0) {
            return false;
        }
        if ($minutes >= 60 || $minutes < 0) {
            return false;
        }
        if ($seconds >= 60 || $seconds < 0) {
            return false;
        }
        return true;
    }
}
?>
Return current item: DATA