PHP Classes
elePHPant
Icontem

File: DATA/SQLDatetime.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Martin Alterisio  >  DATA  >  DATA/SQLDatetime.php  >  Download  
File: DATA/SQLDatetime.php
Role: Class source
Content type: text/plain
Description: ANSI SQL Datetime data type representation.
Class: DATA
Access data stored in MySQL tables like arrays
Author: By
Last change: + anonymous access
Date: 7 years ago
Size: 8,282 bytes
 

Contents

Class file image Download
<?php
/**
 * @package DATA
 */

/**
 * ANSI SQL Datetime data type representation.
 *
 * When inboxing, if the date or time is invalid,
 * {@link DATA_InvalidDatetime} is thrown.
 */
class DATA_SQLDatetime extends DATA_SQLType {
   
/**
     * The date year.
     * @var int
     */
   
protected $year;
   
/**
     * The date month.
     * @var int
     */
   
protected $month;
   
/**
     * The date day.
     * @var int
     */
   
protected $day;
   
/**
     * 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_InvalidDatetime}.
     *
     * @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.
     * @param int $hour The time hour.
     * @param int $minutes The time minutes.
     * @param int $seconds The time seconds.
     */
   
public function __construct($nullable, $year = null, $month = null, $day = null,
                                          
$hour = null, $minutes = null, $seconds = null) {
       
parent::__construct($nullable, is_null($year) || is_null($month) || is_null($day)
                                       ||
is_null($hour) || is_null($minutes) || is_null($seconds));
        if (!
$this->isNull()) {
           
$this->setDate($year, $month, $day);
           
$this->setTime($hour, $minutes, $seconds);
        }
    }
   
   
/**
     * Returns a time object with now's time.
     *
     * @return DATA_SQLDatetime Now's time.
     */
   
public static function now() {
       
$now = time();
       
$year = (int)date("Y", $now);
       
$month = (int)date("n", $now);
       
$day = (int)date("j", $now);
       
$hour = (int)date("G", $now);
       
$minutes = (int)date("i", $now);
       
$seconds = (int)date("s", $now);
        return new
DATA_SQLDatetime(true, $year, $month, $day, $hour, $minutes, $seconds);
    }
   
   
/**
     * Returns the date year.
     *
     * @return int The date year.
     */
   
public function getYear() {
        return
$this->year;
    }
   
   
/**
     * Sets the date year.
     *
     * Throws {@link DATA_InvalidDatetime}.
     *
     * @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_InvalidDatetime}.
     *
     * @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_InvalidDatetime}.
     *
     * @param int $year The date day.
     */
   
public function setDay($day) {
       
$this->setDate($this->year, $this->month, $day);
    }
   
   
/**
     * Returns the time hour.
     *
     * @return int The time hour.
     */
   
public function getHour() {
        return
$this->hour;
    }
   
   
/**
     * Sets the time hour.
     *
     * Throws {@link DATA_InvalidDatetime}.
     *
     * @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_InvalidDatetime}.
     *
     * @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_InvalidDatetime}.
     *
     * @param int $year The time seconds.
     */
   
public function setSeconds($seconds) {
       
$this->setTime($this->hour, $this->minutes, $seconds);
    }
   
   
/**
     * Sets all the date values (year, month, day).
     *
     * Throws {@link DATA_InvalidDatetime}.
     *
     * @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_InvalidDatetime("$year-$month-$day");
        }
       
$this->year = $year;
       
$this->month = $month;
       
$this->day = $day;
       
$this->setNotNull();
    }
   
   
/**
     * Sets all the time values (hour, minutes, seconds).
     *
     * Throws {@link DATA_InvalidDatetime}.
     *
     * @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_InvalidDatetime("$hour:$minutes:$seconds");
        }
       
$this->hour = $hour;
       
$this->minutes = $minutes;
       
$this->seconds = $seconds;
       
$this->setNotNull();
    }
   
    public function
setNull() {
       
parent::setNull();
       
$this->year = null;
       
$this->month = null;
       
$this->day = null;
       
$this->hour = null;
       
$this->minutes = null;
       
$this->seconds = 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)
             .
' '
            
. 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 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"));
    }
   
   
/**
     * 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;
    }
}
?>