Voting

: min(five, eight)?
(Example: nine)

The Note You're Voting On

anonymous at nospam dot com
5 months ago
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

<< Back to user notes page

To Top