/
Using DateTime class
Using DateTime class
In PHP 5.x new DateTime class was added, that eases date operations (see Start using error free date manipulation code). However practice shows, that new compatibility methods, added in kUtil
class, aren't used as they were expected to be used.
If we plan to keep PHP 5.2 compatibility for serious amount of time (e.g. 1 year and up), then I suggest creating kDateTime class, that:
- would have all new methods added in PHP 5.3 (e.g. "getTimestamp", "diff");
- static kDateTime::create method, that would instantiate regular DateTime class (in PHP 5.3+) or this kDateTime class (in PHP 5.0.x - 5.2.x) with all missing methods present.
Here are the methods we're missing in PHP 5.2 (see http://php.net/manual/en/class.datetime.php):
Method Name | Method Description |
---|---|
add | http://www.php.net/manual/en/datetime.add.php |
createFromFormat | http://www.php.net/manual/en/datetime.createfromformat.php |
diff | http://www.php.net/manual/en/datetime.diff.php |
getTimestamp | http://www.php.net/manual/en/datetime.gettimestamp.php |
setTimestamp | http://www.php.net/manual/en/datetime.settimestamp.php |
sub | http://www.php.net/manual/en/datetime.sub.php |
And of course we need to create fake DateInterval class (see http://www.php.net/manual/en/class.dateinterval.php), because it was only added in PHP 5.3.
P.S.
It's not easy task to implement logic in PHP 5.2, that you know ahead is already present in PHP 5.3.