From 7a94745acb1db3aa762a8afb1a53816d0e9c3681 Mon Sep 17 00:00:00 2001 From: Christoph Michael Becker Date: Thu, 23 Jun 2016 16:12:00 +0000 Subject: [PATCH] Fix #72471: Docs to explain how adding/subtracting dates work git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@339459 c90b9560-bf6c-de11-be94-00142212c4b1 --- reference/datetime/book.xml | 1 + reference/datetime/examples.xml | 126 ++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 reference/datetime/examples.xml diff --git a/reference/datetime/book.xml b/reference/datetime/book.xml index c9686e9546..a9e38927d1 100644 --- a/reference/datetime/book.xml +++ b/reference/datetime/book.xml @@ -41,6 +41,7 @@ &reference.datetime.setup; &reference.datetime.constants; + &reference.datetime.examples; &reference.datetime.datetime; &reference.datetime.datetimeimmutable; diff --git a/reference/datetime/examples.xml b/reference/datetime/examples.xml new file mode 100644 index 0000000000..0b742687f1 --- /dev/null +++ b/reference/datetime/examples.xml @@ -0,0 +1,126 @@ + + + + + &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; + + + + + +
+ +
+ +