3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Blar_DateTime extends DateTime { /** * Return Date in ISO8601 format * * @return String */ public function __toString() { return $this->format('Y-m-d H:i'); } /** * Return difference between $this and $now * * @param Datetime|String $now * @return DateInterval */ public function diff($now = 'NOW') { if(!($now instanceOf DateTime)) { $now = new DateTime($now); } return parent::diff($now); } /** * Return Age in Years * * @param Datetime|String $now * @return Integer */ public function getAge($now = 'NOW') { return $this->diff($now)->format('%y'); } } $birthday = new Blar_DateTime('1879-03-14'); // Example 1 echo $birthday; // Result: 1879-03-14 00:00 // Example 2 echo '<p>Albert Einstein would now be ', $birthday->getAge(), ' years old.</p>'; // Result: <p>Albert Einstein would now be 130 years old.</p> // Example 3 echo '<p>Albert Einstein would now be ', $birthday->diff()->format('%y Years, %m Months, %d Days'), ' old.</p>'; // Result: <p>Albert Einstein would now be 130 Years, 10 Months, 10 Days old.</p> // Example 4 echo '<p>Albert Einstein was on 2010-10-10 ', $birthday->getAge('2010-10-10'), ' years old.</p>'; // Result: <p>Albert Einstein was on 2010-10-10 131 years old.</p>
Output for 7.0.0 - 7.0.1
Fatal error: Declaration of Blar_DateTime::diff($now = 'NOW') must be compatible with DateTimeInterface::diff($object, $absolute = NULL) in /in/WXoa8 on line 37
Process exited with code 255.
Output for 5.5.0 - 5.6.16
Fatal error: Declaration of Blar_DateTime::diff() must be compatible with DateTimeInterface::diff($object, $absolute = NULL) in /in/WXoa8 on line 37
Process exited with code 255.
Output for 5.4.0 - 5.4.45
Strict Standards: Declaration of Blar_DateTime::diff() should be compatible with DateTime::diff($object, $absolute = NULL) in /in/WXoa8 on line 37 1879-03-14 00:00<p>Albert Einstein would now be 136 years old.</p><p>Albert Einstein would now be 136 Years, 9 Months, 14 Days old.</p><p>Albert Einstein was on 2010-10-10 131 years old.</p>
Output for 5.3.0 - 5.3.29
Strict Standards: Declaration of Blar_DateTime::diff() should be compatible with that of DateTime::diff() in /in/WXoa8 on line 37 1879-03-14 00:00<p>Albert Einstein would now be 136 years old.</p><p>Albert Einstein would now be 136 Years, 9 Months, 14 Days old.</p><p>Albert Einstein was on 2010-10-10 131 years old.</p>
Output for 5.2.0 - 5.2.17
1879-03-14 00:00<p>Albert Einstein would now be Fatal error: Call to undefined method DateTime::diff() in /in/WXoa8 on line 24
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.1 - 5.1.6
Fatal error: Class 'DateTime' not found in /in/WXoa8 on line 3
Process exited with code 255.
Output for 5.1.0
Fatal error: fatal flex scanner internal error--end of buffer missed in /in/WXoa8 on line 55
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/WXoa8 on line 10
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/WXoa8 on line 10
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/WXoa8 on line 10
Process exited with code 255.