note 109475 modified in class.datetime by yannick

From: Date: Fri, 20 Jul 2012 13:59:05 +0000
Subject: note 109475 modified in class.datetime by yannick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189316@lists.php.net to get a copy of this message
DateTime extension that work with millisecond

<?php
class MilliSecondDateTime extends Datetime{

	private $millisecond;

	public function __construct($time = NULL,$time_zone = NULL){
		
		if(is_null($time))
			$time = '@'.microtime(true);

		if(is_null($time_zone))
			$time_zone = new DateTimeZone('UTC');

		if(preg_match('/@(\\\\d+)\\\\.(\\\\d+)/',$time,$matches)){
			$this->millisecond = $matches[2];
			parent::__construct('@'.$matches[1],$time_zone);
		} else {
			$this->millisecond = 0;
			parent::__construct($time,$time_zone);
		}
	}

	public function getSQLDate(){
		return $this->format('Y-m-d G:i:s').'.'.$this->millisecond;
	}

}
?>

Note that sub() method continue to work with millisecond --was-- DateTime extension that work with millisecond class MilliSecondDateTime extends Datetime{ private $millisecond; public function __construct($time = NULL,$time_zone = NULL){ if(is_null($time)) $time = '@'.microtime(true); if(is_null($time_zone)) $time_zone = new DateTimeZone('UTC'); if(preg_match('/@(\d+)\.(\d+)/',$time,$matches)){ $this->millisecond = $matches[2]; parent::__construct('@'.$matches[1],$time_zone); } else { $this->millisecond = 0; parent::__construct($time,$time_zone); } } public function getSQLDate(){ return $this->format('Y-m-d G:i:s').'.'.$this->millisecond; } } Note that sub() method continue to work with millisecond http://php.net/manual/en/class.datetime.php

« previous php.notes (#189316) next »