<?php
 
/**
 
 * @package DATA
 
 */
 
 
/**
 
 * ANSI SQL Date data type representation.
 
 * 
 
 * When inboxing, if the date is invalid,
 
 * {@link DATA_InvalidDate} is thrown.
 
 * 
 
 * @todo Date math.
 
 */
 
class DATA_SQLDate extends DATA_SQLType {
 
    /**
 
     * The date year.
 
     * @var int
 
     */
 
    protected $year;
 
    /**
 
     * The date month.
 
     * @var int
 
     */
 
    protected $month;
 
    /**
 
     * The date day.
 
     * @var int
 
     */
 
    protected $day;
 
    
 
    /**
 
     * Builds a Date data type with the specified year, month and day, or todays date if not specified.
 
     * 
 
     * Throws {@link DATA_InvalidDate}.
 
     * 
 
     * @param boolean $nullable True if the type is nullable.
 
     * @param int $year The date year. Optional.
 
     * @param int $month The date month. Optional.
 
     * @param int $day The date day. Optional.
 
     */
 
    public function __construct($nullable, $year = null, $month = null, $day = null) {
 
        parent::__construct($nullable, is_null($year) || is_null($month) || is_null($day));
 
        if (!$this->isNull()) {
 
            $this->setDate($year, $month, $day);
 
        }
 
    }
 
    
 
    /**
 
     * Returns a date object with today's date.
 
     * 
 
     * @return DATA_SQLDate Today's date.
 
     */
 
    public static function today() {
 
        $now = time();
 
        $year = (int)date("Y", $now);
 
        $month = (int)date("n", $now);
 
        $day = (int)date("j", $now);
 
        return new DATA_SQLDate(true, $year, $month, $day);
 
    }
 
    
 
    /**
 
     * Returns the date year.
 
     * 
 
     * @return int The date year.
 
     */
 
    public function getYear() {
 
        return $this->year;
 
    }
 
    
 
    /**
 
     * Sets the date year.
 
     * 
 
     * Throws {@link DATA_InvalidDate}.
 
     * 
 
     * @param int $year The date year.
 
     */
 
    public function setYear($year) {
 
        $this->setDate($year, $this->month, $this->day);
 
    }
 
    
 
    /**
 
     * Returns the date month.
 
     * 
 
     * @return int The date month.
 
     */
 
    public function getMonth() {
 
        return $this->month;
 
    }
 
    
 
    /**
 
     * Sets the date month.
 
     * 
 
     * Throws {@link DATA_InvalidDate}.
 
     * 
 
     * @param int $year The date month.
 
     */
 
    public function setMonth($month) {
 
        $this->setDate($this->year, $month, $this->day);
 
    }
 
    
 
    /**
 
     * Returns the date day.
 
     * 
 
     * @return int The date day.
 
     */
 
    public function getDay() {
 
        return $this->day;
 
    }
 
    
 
    /**
 
     * Sets the date day.
 
     * 
 
     * Throws {@link DATA_InvalidDate}.
 
     * 
 
     * @param int $year The date day.
 
     */
 
    public function setDay($day) {
 
        $this->setDate($this->year, $this->month, $day);
 
    }
 
    
 
    /**
 
     * Sets all the date values (year, month, day).
 
     * 
 
     * Throws {@link DATA_InvalidDate}.
 
     * 
 
     * @param int $year The date year.
 
     * @param int $month The date month.
 
     * @param int $day The date day.
 
     */
 
    public function setDate($year, $month, $day) {
 
        if (!$this->checkDate($year, $month, $day)) {
 
            throw new DATA_InvalidDate("$year-$month-$day");
 
        }
 
        $this->year = $year;
 
        $this->month = $month;
 
        $this->day = $day;
 
        $this->setNotNull();
 
    }
 
    
 
    public function setNull() {
 
        parent::setNull();
 
        $this->year = null;
 
        $this->month = null;
 
        $this->day = null;
 
    }
 
    
 
    public function __toString() {
 
        return str_pad($this->year, 4, '0', STR_PAD_LEFT) . '-'
 
             . str_pad($this->month, 2, '0', STR_PAD_LEFT) . '-'
 
             . str_pad($this->day, 2, '0', STR_PAD_LEFT);
 
    }
 
    
 
    /**
 
     * Checks if a given date is valid.
 
     * 
 
     * @param int $year The date year.
 
     * @param int $month The date month.
 
     * @param int $day The date day.
 
     * @return bool True if the date is valid.
 
     */
 
    protected function checkDate($year, $month, $day) {
 
        if ($month > 12 || $month < 1) {
 
            return false;
 
        }
 
        if ($day > self::daysOfMonth($year, $month) || $day < 1) {
 
            return false;
 
        }
 
        return true;
 
    }
 
    
 
    /**
 
     * Returns how many days a month has.
 
     * 
 
     * @param int $year The date year.
 
     * @param int $month The date month.
 
     * @return int Days in the month.
 
     */
 
    protected static function daysOfMonth($year, $month) {
 
        return (int) date('t', strtotime("$year-$month-01"));
 
    }
 
}
 
?>
 
 
 |