Page 2 of 2

Re: PHP5 DateTime object

Posted: Mon Dec 22, 2008 4:54 pm
by Luke
Very much so, thanks! :)

Re: PHP5 DateTime object

Posted: Wed Dec 24, 2008 1:37 am
by Luke
Here's what I've got so far... I thought I'd start simple. I've really only included what I have needed so far in my icalendar library. I'm sure there are a lot of other methods that can be added to it, but I want to keep it as light as possible.

Code: Select all

<?php
/**
 * Date object
 * 
 * @package qCal
 * @subpackage qCal_Date
 * @copyright Luke Visinoni (luke.visinoni@gmail.com)
 * @author Luke Visinoni (luke.visinoni@gmail.com)
 * @license GNU Lesser General Public License
 */
class qCal_Date extends DateTime {
 
    /**
     * UTC format string
     */
    const UTC = "Ymd\THis\Z";
    /**
     * Used in cases where I need a nice formatted date/time for strtotime
     */
    const DATETIME = "Y-m-d H:i:s";
    /**
     * Used in cases where I need a nice formatted date for strtotime
     */
    const DATE = "Y-m-d";
    /**
     * Used in cases where I need a nice formatted time for strtotime
     */
    const TIME = "H:i:s";
    /**
     * Class constructor. This method will accept any date/time format that can be parsed
     * with the strtotime function.
     */
    public function __construct($date = null, $timezone = null) {
    
        if ($date instanceof qCal_Date) {
            // if date object was passed in, copy it
            $date = date(self::DATETIME, $date->time());
        } elseif (ctype_digit($date)) {
            // if numerical, then its probably a unix timestamp, treat it as such
            $timestamp = $date;
            $date = date(self::DATETIME, $timestamp);
        } elseif (is_null($date)) {
            $date = "now";
        }
        // in order to not throw a warning for an invalid date format, I have to check that strtotime works properly here
        if (!$timestamp = strtotime($date)) {
            // if unix timestamp can't be created throw an exception
            throw new qCal_Date_Exception_InvalidDate("Invalid or ambiguous date string passed to qCal_Date::setDate()");
        }
        if (is_null($timezone)) $timezone = new DateTimeZone(date_default_timezone_get());
        parent::__construct($date, $timezone);
    
    }
    /**
     * Returns a unix timestamp
     */
    public function time() {
    
        return $this->format('U');
    
    }
    /**
     * Copy a qCal_Date object into this object
     */
    public function copy(qCal_Date $date) {
    
        $this->setByString($date->format(self::DATETIME));
    
    }
    /**
     * Set the date/time with a string (using strtotime)
     */
    public function setByString($str) {
    
        $date = strtotime($str);
        $this->setDate(date('Y', $date), date('m', $date), date('d', $date));
        $this->setTime(date('H', $date), date('i', $date), date('s', $date));
    
    }
    /**
     * Surprising there is no __toString built-in
     * @todo Test this
     */
    public function __toString() {
    
        return $this->format(self::UTC);
    
    }
 
}