What date/time functions to use for PHP 5.2
The DateTime, DateInterval开发者_如何学Python etc. classes are not available at the server I am using (PHP 5.2)
Which functions should I use instead?
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()
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
.
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
Here's a reference of the date/time functions you can use.
精彩评论