Location: PHPKode > scripts > DateObject > DateObj.php
<?php
/**
 * Class for manipulating date
 * @author Riccardo Vacirca <hide@address.com>
 * @license GNU General Public Licence (GPL)
 * @version 0.1
*/
final class DateObj{
	public $buffer;
	public final function __construct($d=''){
    	$this->buffer = $d ? $d : date('Y-m-d');
    }
	public final function toTime(){
        $d = explode('-', $this->buffer);
        return mktime(0, 0, 0, date($d[1]), date($d[2]), date($d[0]));
    }
	public final function toFormat($f='Y-m-d'){
    	return date($f, $this->toTime());
    }
    public final function reset(){
        return $this->toFormat();
    }
    public final function getYear(){
        return $this->toFormat('Y');
    }
    public final function getMonth(){
        return $this->toFormat('m');
    }
    public final function getDay(){
        return $this->toFormat('d');
    }
    public final function getDayName(){
        return $this->toFormat('D');
    }
	public final function add($y=0, $m=0, $d=0){
        $_d = explode('-', $this->buffer);
        $_t = mktime(0, 0, 0, (date($_d[1]) + $m), (date($_d[2]) + $d), (date($_d[0]) + $y));
        unset($_d);
        return date('Y-m-d', $_t);
    }
	public final function sub($y=0, $m=0, $d=0){
        $_d = explode('-', $this->buffer);
        $_t = mktime(0, 0, 0, (date($_d[1]) - $m), (date($_d[2]) - $d), (date($_d[0]) - $y));
        unset($_d);
        return date('Y-m-d', $_t);
    }
	public final function add_days($n){
    	$this->buffer = $this->add(0, 0, $n);
    }
	public final function add_months($n){
    	$this->buffer = $this->add(0, $n, 0);
    }
	public final function add_years($n){
    	$this->buffer = $this->add($n, 0, 0);
    }
	public final function sub_days($n){
    	$this->buffer = $this->sub(0, 0, $n);
    }
	public final function sub_months($n){
    	$this->buffer = $this->sub(0, $n, 0);
    }
	public final function sub_years($n){
    	$this->buffer = $this->sub($n, 0, 0);
    }
	public final function gt(Date $d){
    	return $this->toTime() > $d->toTime();
    }
	public final function eq(Date $d){
    	return $this->toTime() == $d->toTime();
    }
	public final function lt(Date $d){
    	return $this->toTime() < $d->toTime();
    }
	public final function between(Date $from, Date $to, $left=true, $right=false){
        if($left && !$right)
            if($this->toTime() < $to->toTime())
                return $this->toTime() >= $from->toTime();
        if(!$left && $right)
            if($this->toTime() <= $to->toTime())
                return $this->toTime() > $from->toTime();
        if($left && $right)
            if($this->toTime() <= $to->toTime())
                return $this->toTime() >= $from->toTime();
        if(!$left && !$right)
            if($this->toTime() < $to->toTime())
                return $this->toTime() > $from->toTime();
        return false;
    }
	public final function __toString(){
    	return (string) $this->buffer;
    }
}
?>
Return current item: DateObject