Location: PHPKode > projects > crVCL PHP Framework > datetime.lib.php
<?PHP

/* 

The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html or see MPL-1.1.txt in directory "license"

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.

The Initial Developers of the Original Code are: 
Copyright (c) 2003-2012, CR-Solutions (http://www.cr-solutions.net), Ricardo Cescon
All Rights Reserved.

Contributor(s): Ricardo Cescon

crVCL PHP Framework Version 2.4
*/




############################################################
if(!defined("DATE_LIB")){
   define ("DATE_LIB", 1);
############################################################

class crDateTime
{
// private
	var $m_date;     
	var $m_datePart; 
	var $m_format;   
	
//-------------------------------------------------------------------------------------------------------------------------------------
// constructor
	function crDateTime($dateTime="")
	{
		$this->m_format = "Y-m-d H:i:s";
		if (!$dateTime) 
			$this->setDate(time());
		else
			$this->setDate($dateTime);
	}
//-------------------------------------------------------------------------------------------------------------------------------------		
// private

	function _parseDate($dateTime="")
	{
		$ts = 0; 
		switch(gettype($dateTime))
		{
			case "string": 
				$ts=strtotime($dateTime);	
				break;
			case "integer":
				$ts=$dateTime;
				break;
			case "object":
				if (get_class($dateTime) == "Date")
				{
					$ts = $dateTime->timestamp();
				}
		} 
		return $ts;
	} 
//-------------------------------------------------------------------------------------------------------------------------------------	
// public
	/**
	 * take <$dateTime> and attempt to convert it into a timestamp variable.  If a string then string must be in standard format.  If an integer it must be a valid timestamp variable.  If nothing is passed defaults to current time. 
	 * @param mixed $dateTime
	 * @return self
	 */
	function setDate($dateTime="")
	{
	   if(is_string($dateTime)){
	      $dateTime = dateFormat($dateTime, "Y-m-d H:i:s");
	   }
	   
		$oldStamp = $this->timestamp();
		
		//---------------------------------------------------
		// try to convert $dateTime to a time stamp
		//---------------------------------------------------		
		if ($dateTime=="") 
			$this->m_date=time();
		else
			$this->m_date = $this->_parseDate($dateTime);
		
		//---------------------------------------------------
		//  parse out date/time components if new timestamp
		//  is different than previous timestamp.  This is
		//	done only once per value so that repeated calls
		//	to member functions do no have to repeatedly 
		//  reparse the timestamp
		//---------------------------------------------------
		if ($oldStamp != $this->timestamp())
		{
			$this->m_datePart = getdate($this->timestamp());
		}
		return $this;
	} 
//-------------------------------------------------------------------------------------------------------------------------------------		
	/**
	 * returns specific part of current date/time of class. If $dateTime is passed then $dateTime replaces value of class.
	 *
	 * $datePart must conform to valid parameters of getdate()
	 *		seconds		mday		year		yday
	 *		minutes		wday		month		
	 *		hours		weekday		mon		timestamp
	 * 
	 * @param string $datePart
	 * @param string $dateTime
	 * @return int
	 */
	function datePart($datePart="",$dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
		if ($datePart=="timestamp") $datePart = 0;
		return $this->m_datePart[$datePart];
	} 
//-------------------------------------------------------------------------------------------------------------------------------------			
   /**
    * returns the year of current date/time of class. If $dateTime is passed then $dateTime replaces value of class.
    *
    * @param string $dateTime
    * @return int
    */
	function year($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
		return $this->datePart("year");
	}
//-------------------------------------------------------------------------------------------------------------------------------------			
   /**
    * returns the month of current date/time of class. If $dateTime is passed then $dateTime replaces value of class.
    *
    * @param string $dateTime
    * @return int
    */
	function month($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
		return $this->datePart("mon");
	}
//-------------------------------------------------------------------------------------------------------------------------------------			
   /**
    * returns the day of current date/time of class. If $dateTime is passed then $dateTime replaces value of class.
    *
    * @param string $dateTime
    * @return int
    */
	function day($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
		return $this->datePart("mday");
	}
//-------------------------------------------------------------------------------------------------------------------------------------			
   /**
    * returns the hours of current date/time of class. If $dateTime is passed then $dateTime replaces value of class.
    *
    * @param string $dateTime
    * @return int
    */
	function hours($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
		return $this->datePart("hours");
	}
//-------------------------------------------------------------------------------------------------------------------------------------			
   /**
    * returns the minutes of current date/time of class. If $dateTime is passed then $dateTime replaces value of class.
    *
    * @param string $dateTime
    * @return int
    */
	function minutes($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
		return $this->datePart("minutes");
	}
//-------------------------------------------------------------------------------------------------------------------------------------			
   /**
    * returns the seconds of current date/time of class. If $dateTime is passed then $dateTime replaces value of class.
    *
    * @param string $dateTime
    * @return int
    */
	function seconds($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
		return $this->datePart("seconds");
	}
//-------------------------------------------------------------------------------------------------------------------------------------				
	 /**
	  * adjusts <$adjustPart> of class date by <$adustValue>. (to calculate the next month correctly depending on days of month use the method "NewMonthDate")
	  *
	  * @param string $datePart => corresponding keys from the date() and getdate() functions
	  * @param int $adjustValue => use negative to move backwards in time. 
	  * @param string $dateTime
	  * @return self
	  */
    function calc($datePart, $adjustValue, $dateTime="") 
 	 {
		if ($dateTime) $this->setDate($dateTime);
		if (!is_int($adjustValue)) $adjustValue = 0;
		
		$year = $this->year();
		$month = $this->month();
		$day = $this->day();
		$hour = $this->hours();
		$min = $this->minutes();
		$sec = $this->seconds();
		
		switch(strtolower($datePart))
		{
		case "months":
		case "month": // month
		case "mon":   
		case "m":
 			$this->setDate(mktime($hour,$min,$sec,$month+$adjustValue,$day, $year));
			break;
		case "day": // day
		case "days":
		case "d":
		case "mday":
			$this->setDate(mktime($hour,$min,$sec,$month,$day+$adjustValue, $year));
			break;
		case "year": // year
		case "years":
		case "y":
			$this->setDate(mktime($hour,$min,$sec,$month,$day, $year+$adjustValue));
			break;
		case "hours": // hour
		case "hour":
		case "g": case "G": case "H": case "h":
			$this->setDate(mktime($hour+$adjustValue,$min,$sec,$month,$day, $year));
			break;
		case "minutes": // minute
		case "minute":
		case "i":
			$this->setDate(mktime($hour,$min+$adjustValue,$sec,$month,$day, $year));
			break;
		case "seconds": // seconds
		case "second":
		case "s":
			$this->setDate(mktime($hour,$min,$sec+$adjustValue,$month,$day, $year));
			break;
		} 
		return $this;
    } 
//-------------------------------------------------------------------------------------------------------------------------------------    
    /**
     * to calculate the next month correctly depending on days of month (work like ADD_DATE implementation of MySQL)
     * 
     * @param int $adjustValue => use negative to move backwards in time.     
     * @return self
     */
    function NewMonthDate($adjustValue) {
         $y = $m = $d = 0;
         list($y, $m, $d) = explode('-', $this->toString('Y-m-d'));

         $t = '+';
         if ($adjustValue < 0) {
            $t = '-';
            $adjustValue = -1 * $adjustValue;
         }

         $mm = 0;
         for ($i = 1; $i <= $adjustValue; $i++) {
            if ($t == "+") {
               $pmonth = $m;
            }
            if ($t == "-") {
               $pmonth = $m - $i;
            }

            $pyear = $y;

            $lastmonth = mktime(0, 0, 0, $pmonth, "1", $pyear);

            $smonth = date("m", $lastmonth);
            $syear = date("Y", $lastmonth);
            $ndays = date("t", $lastmonth);

            $valarray = array($smonth, $syear, $ndays);

            $mm = $mm + $ndays;
         }

         // substrate one day (because 1 month peroid is less one day i.e. 2012-01-10 till 2012-01-09)
         $mm = $mm - 1;

         $basedate = $pyear . '-' . $m . '-' . $d;
         $date2 = strtotime($basedate . ' ' . $t . $mm . ' day');
         $date1 = date("Y-m-d", $date2);
         $this->setDate($date1);
         return $this;
      }
//-------------------------------------------------------------------------------------------------------------------------------------        
	/**
	 * return the timestamp
	 *
	 * @param string $dateTime
	 * @return int
	 */
	function timeStamp($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
		return $this->m_date;
	} 
//-------------------------------------------------------------------------------------------------------------------------------------						
	/**
	 * return the beginning of week, returned value is in a new crDateTime instance and does not modify existing class value
	 *
	 * @param string $dateTime
	 * @return crDateTime
	 */
	function BOW($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);

		$dt = new crDateTime($this->timestamp());
		$wday = $dt->datePart("wday");
		$wday = $wday*-1;
		$dt->calc("mday",$wday);
		return $dt;
	} 
//-------------------------------------------------------------------------------------------------------------------------------------						
	/**
	 * return the end of week, returned value is in a new crDateTime instance and does not modify existing class value
	 *
	 * @param string $dateTime
	 * @return crDateTime
	 */	
	function EOW($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
		$dt = new crDateTime($this->timestamp());
		return $dt->calc("day",6-$dt->datePart("wday"));
	}
//-------------------------------------------------------------------------------------------------------------------------------------							
	/**
	 * return the beginning of month, returned value is in a new crDateTime instance and does not modify existing class value
	 *
	 * @param string $dateTime
	 * @return crDateTime
	 */
	function BOM($dateTime="")
	{	
		if ($dateTime) $this->setDate($dateTime);
		$dt = new crDateTime($this->timestamp());
		
		$dt->calc("day",-1*($dt->day()));	
		$dt->calc("day",1);	
		return $dt;
	} 
//-------------------------------------------------------------------------------------------------------------------------------------							
	/**
	 * return the end of month, returned value is in a new crDateTime instance and does not modify existing class value
	 *
	 * @param string $dateTime
	 * @return crDateTime
	 */
	function EOM($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
		$dt = new crDateTime($this->timestamp());
		$dt->calc("month",1);
		$dtBOM=$dt->BOM();
		$dtBOM->calc("day",-1);
		return $dtBOM;
	} 
//-------------------------------------------------------------------------------------------------------------------------------------							
	/**
	 * return the beginning of year, returned value is in a new crDateTime instance and does not modify existing class value
	 *
	 * @param string $dateTime
	 * @return crDateTime
	 */	
	function BOY($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
	
		$dt = new crDateTime($this->timestamp());
		$dt->calc("month",-1*($dt->month()-1));
		return $dt->BOM();	
	} 
//-------------------------------------------------------------------------------------------------------------------------------------							
	/**
	 * return the end of year, returned value is in a new crDateTime instance and does not modify existing class value
	 *
	 * @param string $dateTime
	 * @return crDateTime
	 */	
	function EOY($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);	
		$dt = new crDateTime($this->timestamp());
		$dt->calc("year",1);
		$dtEOY = $dt->BOY();
		$dtEOY->calc("day",-1);
		return $dtEOY;
	} 
//-------------------------------------------------------------------------------------------------------------------------------------								
	/**
	 * return the quarter (1-4) for current value of the class
	 *
	 * @param string $dateTime
	 * @return int
	 */
	function quarter($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);		
		switch($this->month())
		{
		case 1: case 2: case 3: return 1;break;
		case 4: case 5: case 6: return 2; break;
		case 7: case 8: case 9: return 3; break;
		case 10: case 11: case 12: return 4;break;
		}
	} 
//-------------------------------------------------------------------------------------------------------------------------------------									
   /**
    * return first day of <$quarter> or the quarter of the current value of the class, returned value is in a new crDateTime instance and does not modify existing class value
    *
    * @param int $quarter
    * @param string $dateTime
    * @return crDateTime
    */
	function BOQ($quarter=0,$dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
		if (!$quarter) $quarter = $this->Quarter();
		switch($quarter)
		{
			case 1:  $month = 1;break;
			case 2:  $month = 4; break;
			case 3:  $month = 7; break;
			case 4:  $month = 10;break;
		}		
		$dt = new crDateTime(mktime($this->hours(),$this->minutes(),$this->seconds(),$month,1,$this->year()));
		return $dt;
	}
//-------------------------------------------------------------------------------------------------------------------------------------										
   /**
    * return last day of <$quarter> or the quarter of the current value of the class, returned value is in a new crDateTime instance and does not modify existing class value
    *
    * @param int $quarter
    * @param string $dateTime
    * @return crDateTime
    */
	function EOQ($quarter=0,$dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);

		if (!$quarter) $quarter = $this->Quarter();
		$month = $quarter*3;
		$dt = new crDateTime(mktime($this->hours(),$this->minutes(),$this->seconds(),$month,1,$this->year()));
		$month = $dt->EOM();
		return $month;			
	} 
//-------------------------------------------------------------------------------------------------------------------------------------										
	/**
	 * return a formated string of the current date
	 *
	 * @param string $format
	 * @param string $dateTime
	 * @return string
	 */
	function toString($format="",$dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
		if($format!="") $this->m_format = $format;
		switch (strtoupper($format))
		{
			case "RFC822": return date("r",$this->timestamp());
			case "RSS_1.0": case "dc:date":
				return date("Y-m-d\TH:i:sO",$this->timestamp());
			case "HTTP": case "RFC1123":
				return date('D, d M Y H:i:s \G\M\T',$this->timestamp());
		}
		return date($this->m_format,$this->timestamp());
	} 
//-------------------------------------------------------------------------------------------------------------------------------------											
	/**
	 * return UTC equivelent time for class date value, returned value is in a new crDateTime instance and does not modify existing class value
	 *
	 * @param string $dateTime
	 * @return crDateTime
	 */
	function UTC($dateTime="")
	{
		if ($dateTime) $this->setDate($dateTime);
		$dt = new crDateTime($this);
		$dt->calc("minutes",date("Z",$this->timestamp()));
		return $dt;
	}
//-------------------------------------------------------------------------------------------------------------------------------------	
} 
//-------------------------------------------------------------------------------------------------------------------------------------
############################################################
}
############################################################
?>
Return current item: crVCL PHP Framework