Question

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 ( )
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top