Page 1 of 2

PHP5 DateTime object

Posted: Sun Dec 14, 2008 9:09 pm
by Luke
Has anybody used the DateTime object in PHP5? I was reading about it here http://us.php.net/manual/en/function.date-create.php and it seems pretty buggy. I'm trying to figure out how to implement dates, times and timezones in my icalendar library and I'm having kind of a hard time. I have to support "floating" (not bound to any time zone) date/times as well as dates with UTC. Any advice?

Re: PHP5 DateTime object

Posted: Sun Dec 14, 2008 9:17 pm
by infolock
Dealing with date and time control is always the subject I hate the most. There is just so much crap involved with it that it can drive a man insane.

Anyways, I've looked into it myself, but always ended up using the old method of mktime, date, strtotime, etc...

Here is a tutorial though if you haven't already read it. Seems to cover some of the issues you are talking about..

Re: PHP5 DateTime object

Posted: Sun Dec 14, 2008 9:32 pm
by Luke
Yea I found that on the php site. I am really curious though if it's even worth bothering with the DateTime object because for one thing, it causes my required php version to jump up into the 5.2x area, when right now it's only php5 and this is a distributed library, so minimum PHP version is important.

One thing I'm having trouble with right now is simply this... there is no way to format a date/time like this with php's date function:

Code: Select all

19980119T070000Z
This, apparently, is UTC time (as noted by the Z at the end). I can do the date part, but the time is in a weird format. 070000? :( :?

Re: PHP5 DateTime object

Posted: Sun Dec 14, 2008 11:12 pm
by infolock
well, the only way you can do it is to convert it yourself. Here is a page that explains what UTC is and how to convert.

It really shouldn't be too hard to follow and write your own routine. maybe an hour or so. but i'd recommend just using the built in ones for now. Apparently the 5.2 upgrade fixed a bunch of issues with it, and the 5.3 was to fix more til they found the security crap with it. anyways, hope this helps ya bro. hell, i may even write one myself now ;) heh


ps: i'm pretty sure that 19980119T070000Z is basically just 01 19, 1998 07:00:00am. it's basically a 24 hour scale, so 4pm would be 160000 (or 16 hours, 0 minutes, 0 seconds).

Re: PHP5 DateTime object

Posted: Mon Dec 15, 2008 12:24 am
by Christopher
Hey Ninja, feel like writing a really good DateTime object? (hint hint) ;)

Re: PHP5 DateTime object

Posted: Mon Dec 15, 2008 11:50 am
by Luke
I'm considering it.

Re: PHP5 DateTime object

Posted: Mon Dec 15, 2008 12:14 pm
by Mark Baker
The Ninja Space Goat wrote:Has anybody used the DateTime object in PHP5?
Avoid like the plague unless you absolutely need to worj with dates that fall outside of the normal PHP date range.

Work with all your dates as UTC, and adjust accordingly from input/for output

Re: PHP5 DateTime object

Posted: Mon Dec 15, 2008 1:04 pm
by Christopher
Mark Baker wrote:Avoid like the plague unless you absolutely need to worj with dates that fall outside of the normal PHP date range.

Work with all your dates as UTC, and adjust accordingly from input/for output
I think that might be exactly what an improved DateTime object would do.
The Ninja Space Goat wrote:I'm considering it.
I show the DateTime class having the following methods:

Code: Select all

DateTime::__construct  ([ string $time  [, DateTimeZone $timezone  ]] )
DateTime::format  ( string $format  ) // Returns date formatted according to given format (same as accepted by date()). 
DateTime::modify  ( string $modify  ) // Alter the timestamp of a DateTime object by incrementing or decrementing in a format accepted by strtotime(). 
DateTime::getTimezone  ( void  ) // Return time zone relative to given DateTime
DateTime::setTimezone  ( DateTimeZone $timezone  )
DateTime::getOffset  ( void  ) // Returns the daylight saving time offset
DateTime::setTime  ( int $hour  , int $minute  [, int $second  ] ) // sets the current time of the DateTime object to a different time. 
DateTime::setDate  ( int $year  , int $month  , int $day  ) // sets the current date of the DateTime object to a different date. 
DateTime::setISODate  ( int $year  , int $week  [, int $day  ] ) // Set a date according to the ISO 8601 standard - using weeks and day offsets rather than specific dates.
There is also a function that takes a date and an interval. I could not find the interface for the DateInterval class.

Code: Select all

date_sub  ( DateTime $object  , DateInterval $interval  )
There is a DateTimeZone class:

Code: Select all

DateTimeZone::__construct  ( string $timezone  )
DateTimeZone::getName()
DateTimeZone::getOffset()
DateTimeZone::getTransitions()
DateTimeZone::listAbbreviations()
DateTimeZone::listIdentifiers()
I find the majority of my general use cases are going back and forth between user typed dates and yyyy-mm-dd for databases. These classes seem to do no date parsing. Then use cases usually are time zone related. It might be good to just extend this class with reasonable additions and override anything that needs fixing.

Re: PHP5 DateTime object

Posted: Mon Dec 15, 2008 1:16 pm
by Luke
yea that may be a good idea. I'll play around with it a bit tonight and let you guys know how it goes.

Re: PHP5 DateTime object

Posted: Fri Dec 19, 2008 1:06 am
by Luke
How about Pear's Date package? Anybody used it? It actually looks pretty decent (although I must admit I only gave it a quick look-over). I think what I'm going to do is use PHP5's DateTime object along with Pear's Date package as inspiration for my own DateTime class/mini-library. One thing I'm not really sure about yet is how I should store the dates/times internally. Although generally unix timestamps are what PHP is most easily able to work with, they limit you to a sort of small range of dates. What do you guys think would be the best way to store a date / time internally?

Re: PHP5 DateTime object

Posted: Fri Dec 19, 2008 2:02 am
by Christopher
The PEAR Date class seems horribly over done with all of those getters and setters. It seems like there could be much smaller interface that still did all of that. And some if it could be split off into several object to do specific obscure stuff like DST or "standard" dates, etc.

If you don't use timestamps then you should probably follow what getdate() or mktime() do, since you will be using them. The many values could probably just be properties of the object (real or virtual). I am interested in this kind of class too.

Re: PHP5 DateTime object

Posted: Mon Dec 22, 2008 8:00 am
by Luke
I'm looking at the result of a dump of the "timezone_abbreviations_list" function. What is the deal with this list? I understand most of it, but I don't understand what the MAIN keys are. Here is a portion of what it returns:

Code: Select all

 ["orast"]=>
  array(1) {
    [0]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(18000)
      ["timezone_id"]=>
      string(9) "Asia/Oral"
    }
  }
  ["orat"]=>
  array(2) {
    [0]=>
    array(3) {
      ["dst"]=>
      bool(false)
      ["offset"]=>
      int(14400)
      ["timezone_id"]=>
      string(9) "Asia/Oral"
    }
    [1]=>
    array(3) {
      ["dst"]=>
      bool(false)
      ["offset"]=>
      int(18000)
      ["timezone_id"]=>
      string(9) "Asia/Oral"
    }
  }
  ["pddt"]=>
  array(1) {
    [0]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-21600)
      ["timezone_id"]=>
      string(14) "America/Inuvik"
    }
  }
  ["pdt"]=>
  array(16) {
    [0]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(19) "America/Los_Angeles"
    }
    [1]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(13) "America/Boise"
    }
    [2]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(14) "America/Dawson"
    }
    [3]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(20) "America/Dawson_Creek"
    }
    [4]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(16) "America/Ensenada"
    }
    [5]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(14) "America/Inuvik"
    }
    [6]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(14) "America/Juneau"
    }
    [7]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(15) "America/Tijuana"
    }
    [8]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(17) "America/Vancouver"
    }
    [9]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(18) "America/Whitehorse"
    }
    [10]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(14) "Canada/Pacific"
    }
    [11]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(12) "Canada/Yukon"
    }
    [12]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(16) "Mexico/BajaNorte"
    }
    [13]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(7) "PST8PDT"
    }
    [14]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(10) "US/Pacific"
    }
    [15]=>
    array(3) {
      ["dst"]=>
      bool(true)
      ["offset"]=>
      int(-25200)
      ["timezone_id"]=>
      string(14) "US/Pacific-New"
    }
  }
 
I understand all of the keys in each time zone (dst = daylight savings time, offset = time offset, id is obvious), but what are the keys that separate the time zones? What is "orast" and "orat" and "pdt"? :?

I was going to use this list to pull out offset and daylight savings info based on the timezone_id, but the way this array is assembled makes it difficult. I have tried to find more information on the function, but docs are sparse :(

Re: PHP5 DateTime object

Posted: Mon Dec 22, 2008 8:12 am
by Mark Baker
pdt = Pacific Daylight Time

Re: PHP5 DateTime object

Posted: Mon Dec 22, 2008 8:13 am
by Luke
Do you have any idea why this list is organized the way it is? I don't get it.

Re: PHP5 DateTime object

Posted: Mon Dec 22, 2008 8:47 am
by Mark Baker
The Ninja Space Goat wrote:Do you have any idea why this list is organized the way it is? I don't get it.
ORAT and ORAST are Oral Time and Oral Summer Time, related to a region of Asia

http://astronomy.physics.tamu.edu/Java/ ... zones.html might provide some clues