PHP5 DateTime object
Moderator: General Moderators
PHP5 DateTime object
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
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..
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
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:
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?

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
19980119T070000ZRe: PHP5 DateTime object
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).
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
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).
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: PHP5 DateTime object
Hey Ninja, feel like writing a really good DateTime object? (hint hint) 
(#10850)
Re: PHP5 DateTime object
I'm considering it.
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: PHP5 DateTime object
Avoid like the plague unless you absolutely need to worj with dates that fall outside of the normal PHP date range.The Ninja Space Goat wrote:Has anybody used the DateTime object in PHP5?
Work with all your dates as UTC, and adjust accordingly from input/for output
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: PHP5 DateTime object
I think that might be exactly what an improved DateTime object would do.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 show the DateTime class having the following methods:The Ninja Space Goat wrote:I'm considering it.
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.Code: Select all
date_sub ( DateTime $object , DateInterval $interval )Code: Select all
DateTimeZone::__construct ( string $timezone )
DateTimeZone::getName()
DateTimeZone::getOffset()
DateTimeZone::getTransitions()
DateTimeZone::listAbbreviations()
DateTimeZone::listIdentifiers()(#10850)
Re: PHP5 DateTime object
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
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?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: PHP5 DateTime object
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.
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.
(#10850)
Re: PHP5 DateTime object
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:
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
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 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
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: PHP5 DateTime object
pdt = Pacific Daylight Time
Re: PHP5 DateTime object
Do you have any idea why this list is organized the way it is? I don't get it.
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: PHP5 DateTime object
ORAT and ORAST are Oral Time and Oral Summer Time, related to a region of AsiaThe Ninja Space Goat wrote:Do you have any idea why this list is organized the way it is? I don't get it.
http://astronomy.physics.tamu.edu/Java/ ... zones.html might provide some clues