note 98005 added to class.datetime

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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) { }
  }
}
----
Server IP: 195.221.21.36
Probable Submitter: 81.252.87.41
----
Manual Page -- http://www.php.net/manual/en/class.datetime.php
Edit        -- https://master.php.net/note/edit/98005
Del: integrated  -- https://master.php.net/note/delete/98005/integrated
Del: useless     -- https://master.php.net/note/delete/98005/useless
Del: bad code    -- https://master.php.net/note/delete/98005/bad+code
Del: spam        -- https://master.php.net/note/delete/98005/spam
Del: non-english -- https://master.php.net/note/delete/98005/non-english
Del: in docs     -- https://master.php.net/note/delete/98005/in+docs
Del: other reasons-- https://master.php.net/note/delete/98005
Reject      -- https://master.php.net/note/reject/98005
Search      -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.