note 130008 added to class.dateinterval
| From: | anonymous@nospam.com | Date: | Mon, 03 Feb 2025 18:19:42 +0000 |
| Subject: | note 130008 added to class.dateinterval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221828@lists.php.net to get a copy of this message | ||
Subtracting months may not work as you might expect.
Code shows how subtracting 2 months from consecutive days produces potentially unexpected results.
<?php
$origin = date_create('2025-01-31T00:00:00');
$target = date_create('2024-11-26T00:00:00');
$interval = date_diff($origin, $target);
echo "interval total days: ".$interval->format('%a days');
echo "interval object years: ".$interval->format('%y years');
echo "interval object months: ".$interval->format('%m months');
echo "interval object days: ".$interval->format('%d days');
// create 3 dates that are consecutive, but in two months
$d1 = date_create('2025-01-31T00:00:00');
$d2 = date_create('2025-02-01T00:00:00');
$d3 = date_create('2025-02-02T00:00:00');
// add (negative) interval object to each date
$d1->add($interval);
$d2->add($interval);
$d3->add($interval);
echo "Add (negative) interval object to each date. Non-consecutive results: ";
echo($d1->format("Y-m-d\TH:i:s"));
echo($d2->format("Y-m-d\TH:i:s"));
echo($d3->format("Y-m-d\TH:i:s"));
// reset dates
$d1 = date_create('2025-01-31T00:00:00');
$d2 = date_create('2025-02-01T00:00:00');
$d3 = date_create('2025-02-02T00:00:00');
// Use same dates, but subtract total number of days instead of adding interval object: ";
$d1->sub(new DateInterval('P'.$interval->format('%a').'D'));
$d2->sub(new DateInterval('P'.$interval->format('%a').'D'));
$d3->sub(new DateInterval('P'.$interval->format('%a').'D'));
echo "Use same dates, but subtract total number of days instead of adding interval object.
Results (consecutive): ";
echo($d1->format("Y-m-d\TH:i:s"));
echo($d2->format("Y-m-d\TH:i:s"));
echo($d3->format("Y-m-d\TH:i:s"));
$d1 = date_create('2025-01-31T00:00:00');
$d2 = date_create('2025-02-01T00:00:00');
$d3 = date_create('2025-02-02T00:00:00');
// do month separately for 2025-02-01T00:00:00
echo "Do month separately (interesting): ";
echo ('Subtract '. $interval->format('%m').' months (might not expect
this):');
echo ('Jan 31 - 2 months (61 days = Dec, 31 days + Nov, 30 days) ==> Dec 1');
echo ('Feb 1 - 2 months (62 days = Dec, 31 days + Jan, 31 days) ==> Dec 1');
$d1->sub(new DateInterval('P'.$interval->format('%m').'M'));
echo $d1->format("Y-m-d\TH:i:s");
$d2->sub(new DateInterval('P'.$interval->format('%m').'M'));
echo $d2->format("Y-m-d\TH:i:s");
$d3->sub(new DateInterval('P'.$interval->format('%m').'M'));
echo $d3->format("Y-m-d\TH:i:s");
?>
Output:
interval total days: 66 days
interval object years: 0 years
interval object months: 2 months
interval object days: 5 days
create 3 dates that are consecutive, but in two months:
$d1 = 2025-01-31T00:00:00
$d2 = 2025-02-01T00:00:00
$d3 = 2025-02-02T00:00:00
Add (negative) interval object to each date. Non-consecutive results:
2024-11-26T00:00:00
2024-11-26T00:00:00
2024-11-27T00:00:00
Use same dates, but subtract total number of days instead of adding interval object. Results
(consecutive):
2024-11-26T00:00:00
2024-11-27T00:00:00
2024-11-28T00:00:00
Do month separately (interesting):
Subtract 2 months (might not expect this):
Jan 31 - 2 months (61 days = Dec, 31 days + Nov, 30 days) ==> Dec 1
Feb 1 - 2 months (62 days = Dec, 31 days + Jan, 31 days) ==> Dec 1
2024-12-01T00:00:00
2024-12-01T00:00:00
2024-12-02T00:00:00
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.84.18 (proxied: 72.160.109.170)
----
Manual Page -- https://php.net/manual/en/class.dateinterval.php
Edit -- https://main.php.net/note/edit/130008
Del: integrated -- https://main.php.net/note/delete/130008/integrated
Del: useless -- https://main.php.net/note/delete/130008/useless
Del: bad code -- https://main.php.net/note/delete/130008/bad+code
Del: spam -- https://main.php.net/note/delete/130008/spam
Del: non-english -- https://main.php.net/note/delete/130008/non-english
Del: in docs -- https://main.php.net/note/delete/130008/in+docs
Del: other reasons-- https://main.php.net/note/delete/130008
Reject -- https://main.php.net/note/reject/130008
Search -- https://main.php.net/manage/user-notes.php