DateTime类在PHP / Apache中不起作用

The DateTime class (as well as DatePeriod and DateInterval) seem to not work on my current server. PHP 5.3.3 is installed per the phpinfo page, and when I run the get_declared_classes() function as an array to see what classes are defined, those classes do appear to exist:

Array ( [0] => stdClass [1] => Exception [2] => ErrorException [3] => Closure [4] => DateTime [5] => DateTimeZone [6] => DateInterval [7] => DatePeriod [8] => LibXMLError...

However, even if I run the simplest of PHP code with DateTime, I will get an HTTP 500 error:

$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');

This older notation does work properly,

$date = date_create('2000-01-01');
echo date_format($date, 'Y-m-d H:i:s');

But for some reason DateTime won't function. I wrote some code using these DateTime objects on a different server and it worked fine. Thoughts as to where I may have an error? Is there a php.ini or other setting I should be considering?

-JF

My best guess is you are using it in a class with namespace and thus DateTime needs an \ Please post a bit more code of your class so we can help you more.

So this is how it would work probably (mind the backslash \)

$date = new \DateTime('2000-01-01');

Also please post you error message for the 500. you'll probably find it in error.log of your server.

I had a similar problem here...

It was the default time zone in php.ini, you can either use date_default_timezone_set() in your script or change it in the php.ini