tags:

views:

76

answers:

4

The DateTime, DateInterval etc. classes are not available at the server I am using (PHP 5.2)

Which functions should I use instead?

+3  A: 

DateTime is part of PHP 5.2 core; however 5.3 added some more methods that weren't available in 5.2

DateInterval, on the other hand, appears to be unique to PHP 5.3.

Are you sure DateTime isn't available at all? Are you sure you're using PHP 5.2?

Try calling class_exists('DateTime') and phpversion()

Frank Farmer
+1  A: 

Here's a reference of the date/time functions you can use.

henasraf
A: 

Note: Experimental DateTime support in PHP 5.1.x Although the DateTime class (and related functions) are enabled by default since PHP 5.2.0, > it is possible to add experimental support into PHP 5.1.x by using the following flag before configure/compile: CFLAGS=-DEXPERIMENTAL_DATE_SUPPORT=1

As can be read on php.net So DateTime should be available on 5.2.0

Tobias
+2  A: 

If you look at the PHP Manual pages for DateTime and DateInterval, you can see which version is required for a specific function right below the method name, e.g.

DateInterval::createFromDateString
(PHP 5 >= 5.3.0)

indicates this method is available from PHP5.3 on.

Like already pointed out in other answers, some of the DateTime (but not DateInterval) methods are already regularly available as of 5.2. As an alternative, you can use the regular Date functions. Note that many of the functions prefixed with date_ and timezone_ are aliases for their respective OOP counterparts in DateTime and DateTimezone, so you will not be able to use them, if you cannot use them now.

If you want an OO lib for dates, have a look at Zend_Date or PEAR_Date.

Gordon
Actually, DateTime::modify($string) works nicely with servers running PHP<5.3 Solved my problem.
Crimson
Am I the only one to find that being presented with a list of 50 functions/methods in the PHP doc and having to click each one in order to know whether it’s available in PHP 5.2 is utterly ridiculous?
Ölbaum