One of the annoying things about php’s DateTime class is that it doesn’t have a __toString() method, meaning that it throws an error every time you try to use it in a string context. So I extended it:
class ZDateTime extends DateTime {
private $zformat='Y-m-d H:i:s';
function __toString() {
return $this->format($this->zformat);
}
function set_format($format){
$this->zformat=$format; //leaving out format validation for brevity
}
}
But now I’ve lost all the nice procedural functions that DateTime has like date_create() and many of the other functions listed here.
I know that the procedural functions are just aliases for DateTime class methods. But they are often easier to use and are liberally sprinkled throughout my code. Fishing them out will be a severe pain. So, any idea how I can make date_create() return a ZDateTime object?
The easiest way is probably just to create a
zdate_createfunction and do a mass search & replace.It can be done, but the only way you can do that in vanilla PHP (i.e.: without runkit and such) is:
ZDateTime.php
Some_Script.php