tags:

views:

44

answers:

1

I'm attempting to use the PHP DateTime class on a Solaris server with PHP 5.2.6, while testing on my local Windows box running PHP 5.3. Here is the code:

<?php
try {
$dt = new DateTime('2009-10-08');
$dt->setDate(2009,10,8);
print_r($dt);
}catch (Exception $e) {
    echo $e->getMessage();
}

On the test server, things work flawlessly, this is what prints:

DateTime Object ( [date] => 2009-10-08 00:00:00 [timezone_type] => 3 [timezone] => America/New_York )

On the server I need to use it on, however, this is what prints:

DateTime Object ( )

Removing the setDate makes no difference whatsoever.

Any ideas why this might be happening?

Thanks!

Edit

Modified the script:

try {
echo '|', date_default_timezone_get(), '|';
date_default_timezone_set('America/New_York');
echo '|', date_default_timezone_get(), '|';
$dt = new DateTime('2009-10-08');
$dt->setDate(2009,10,8);
print_r($dt);
}catch (Exception $e) {
    echo $e->getMessage();
}

New output on the server:

|US/Eastern||America/New_York|DateTime Object ( )
+1  A: 

For one, PHP 5.3 makes you set a default timezone in your php.ini, something which most php 5.2 installations completely ignore. try setting it manually:

date_default_timezone_set("America/New_York");

Also, for the record, there are a few DateTime methods missing in 5.2 (get/set timestamp), you can find workarounds here

Alex
I knew about the missing methods - I've just been avoiding them.Unfortunately, setting the default timezone made no difference :(
Nate Wagar
just out of curiosity here, have you actually tested that the class isnt working? have you compared their actually functionality? the empty dump doesn't necessarily mean its not working... :)
Alex
Honestly, I'm not sure I understand or know how to do what you're describing.
Nate Wagar
I was being dense, the class actually was working, just as it's 5.2 it was incomplete.
Nate Wagar