PHP Classes
Icontem

File: DATA/SQLTime.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Martin Alterisio  >  DATA  >  DATA/SQLTime.php  
File: DATA/SQLTime.php
Role: Class source
Content type: text/plain
Description: ANSI SQL Time data type representation.
Class: DATA
Access data stored in MySQL tables like arrays
 

Contents

Class file image Download
<?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($nullableis_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->hour2'0'STR_PAD_LEFT) . ':'
             
str_pad($this->minutes2'0'STR_PAD_LEFT) . ':'
             
str_pad($this->seconds2'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;
    }
}
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products