Date/Time Arithmetic

The following examples show some pitfalls of Date/Time arithmetic with regard to DST transitions and months having different numbers of days.

Example #1 DateTimeImmutable::add/sub add intervals which cover elapsed time

Adding PT24H over a DST transition will appear to add 23/25 hours (for most timezones).

<?php
$dt 
= new DateTimeImmutable("2015-11-01 00:00:00", new DateTimeZone("America/New_York"));
echo 
"Start: "$dt->format("Y-m-d H:i:s P"), PHP_EOL;
$dt $dt->add(new DateInterval("PT3H"));
echo 
"End:   "$dt->format("Y-m-d H:i:s P"), PHP_EOL;
?>

The above example will output:

Start: 2015-11-01 00:00:00 -04:00
End:   2015-11-01 02:00:00 -05:00

Example #2 DateTimeImmutable::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).

<?php
$dt 
= new DateTimeImmutable("2015-11-01 00:00:00", new DateTimeZone("America/New_York"));
echo 
"Start: "$dt->format("Y-m-d H:i:s P"), PHP_EOL;
$dt $dt->modify("+24 hours");
echo 
"End:   "$dt->format("Y-m-d H:i:s P"), PHP_EOL;
?>

The above example will output:

Start: 2015-11-01 00:00:00 -04:00
End:   2015-11-02 00:00:00 -05:00

Example #3 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).

<?php
echo "Normal year:\n"// February has 28 days
$dt = new DateTimeImmutable("2015-01-31 00:00:00", new DateTimeZone("America/New_York"));
echo 
"Start: "$dt->format("Y-m-d H:i:s P"), PHP_EOL;
$dt $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 DateTimeImmutable("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 $dt->modify("+1 month");
echo 
"End:   "$dt->format("Y-m-d H:i:s P"), PHP_EOL;
?>

The above example will output:

Normal year:
Start: 2015-01-31 00:00:00 -05:00
End:   2015-03-03 00:00:00 -05:00
Leap year:
Start: 2016-01-31 00:00:00 -05:00
End:   2016-03-02 00:00:00 -05:00

To get the last day of the next month (i.e. to prevent the overflow), the last day of format is available.

<?php
echo "Normal year:\n"// February has 28 days
$dt = new DateTimeImmutable("2015-01-31 00:00:00", new DateTimeZone("America/New_York"));
echo 
"Start: "$dt->format("Y-m-d H:i:s P"), PHP_EOL;
$dt $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 DateTimeImmutable("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 $dt->modify("last day of next month");
echo 
"End:   "$dt->format("Y-m-d H:i:s P"), PHP_EOL;
?>

The above example will output:

Normal year:
Start: 2015-01-31 00:00:00 -05:00
End:   2015-02-28 00:00:00 -05:00
Leap year:
Start: 2016-01-31 00:00:00 -05:00
End:   2016-02-29 00:00:00 -05:00

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Definition of stored procedures - an introduction

Stored procedures are predefined SQL code blocks that are stored in a database and can be called up as required. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial