note 98005 deleted from class.datetime by danbrown

From: Date: Thu, 20 May 2010 15:46:36 +0000
Subject: note 98005 deleted from class.datetime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169459@lists.php.net to get a copy of this message
Note Submitter: frosty dot z at freesbee dot fr ---- Completed tom's DateTime for PHP 5.1 (and possibly earlier) Still not perfect, still no timezone handling, but added minimal support for DateTime::setTime(), DateTime::setDate(), DateTime::add(), Datetime::sub(), and DateInterval::__construct(). <?php if (!class_exists('DateTime')) { class DateTime { var $date; function __construct($date = "now", $tz = null) { $this->date = strtotime($date); } function setTime($hour, $minute, $second = 0) { $this->date = strtotime(date("Y-m-d", $this->date)." ".$hour.":".$minute.":".$second); } function setDate($year, $month, $day) { $this->date = strtotime($year."-".$month."-".$day." ".date("H:i:s", $this->date)); } function add(DateInterval $interval) { self::modify('+'.$interval->sec.' sec'); } function sub(DateInterval $interval) { self::modify('-'.$interval->sec.' sec'); } function setTimeZone($timezone) { return; } function modify($multiplier) { $this->date = strtotime($this->__getDate() . ' ' . $multiplier); } function format($format) { return date($format, $this->date); } private function __getDate() { return date(DATE_ATOM, $this->date); } } class DateInterval { var $sec; function __construct($string) { $string = strtoupper($string); if ($string{0} != 'P') throw new Exception("'P' expected as first character in '".$string."'"); $tmp = explode("T", $string, 2); if (count($tmp) == 1) $tmp[] = ''; list($str_date, $str_time) = $tmp; $this->sec = 0; if (preg_match('/([0-9]+)Y/', $str_date, $matches)) $this->sec += $matches[1]; $this->sec *= 12; if (preg_match('/([0-9]+)M/', $str_date, $matches)) $this->sec += $matches[1]; $this->sec *= 365.25; if (preg_match('/([0-9]+)W/', $str_date, $matches)) $this->sec += $matches[1] * 7; if (preg_match('/([0-9]+)D/', $str_date, $matches)) $this->sec += $matches[1]; $this->sec *= 24; if (preg_match('/([0-9]+)H/', $str_time, $matches)) $this->sec += $matches[1]; $this->sec *= 60; if (preg_match('/([0-9]+)M/', $str_time, $matches)) $this->sec += $matches[1]; $this->sec *= 60; if (preg_match('/([0-9]+)S/', $str_time, $matches)) $this->sec += $matches[1]; } } class DateTimeZone { function __construct($string) { } } }

« previous php.notes (#169459) next »