&reftitle.examples;
DateTime Arithmetic The following examples show some pitfalls of DateTime arithmetic with regard to DST transitions and months having different numbers of days. DateTime::add/sub add intervals which cover elapsed time Adding PT24H over a DST transition will appear to add 23/25 hours (for most timezones). format("Y-m-d H:i:s P"), PHP_EOL; $dt->add(new DateInterval("PT3H")); echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL; ?> ]]> &example.outputs; DateTime::modify and strtotime increment or decrement individual component values Adding +24 hours over a DST transition will add exactly 24 hours as seen in the date/time string (unless the start or end time is on a transition point). format("Y-m-d H:i:s P"), PHP_EOL; $dt->modify("+24 hours"); echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL; ?> ]]> &example.outputs; Adding or subtracting times can over- or underflow dates Like where January 31st + 1 month will result in March 2nd (leap year) or 3rd (normal year). format("Y-m-d H:i:s P"), PHP_EOL; $dt->modify("+1 month"); echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL; echo "Leap year:\n"; // February has 29 days $dt = new DateTime("2016-01-31 00:00:00", new DateTimeZone("America/New_York")); echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL; $dt->modify("+1 month"); echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL; ?> ]]> &example.outputs; To get the last day of the next month (i.e. to prevent the overflow), the last day of format is available as of PHP 5.3.0. format("Y-m-d H:i:s P"), PHP_EOL; $dt->modify("last day of next month"); echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL; echo "Leap year:\n"; // February has 29 days $dt = new DateTime("2016-01-31 00:00:00", new DateTimeZone("America/New_York")); echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL; $dt->modify("last day of next month"); echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL; ?> ]]> &example.outputs;