briannesbitt / Carbon

A simple PHP API extension for DateTime.
https://carbon.nesbot.com/
MIT License
16.53k stars 1.28k forks source link

Incorrect totalSeconds after adding interval to CarbonInterval in v3 #2979

Closed jamesRUS52 closed 5 months ago

jamesRUS52 commented 5 months ago

Hello,

I encountered an issue with the following code:

$t1 = now();
$t2 = now()->addMinutes(2);

$p = $t1->diffAsCarbonInterval($t2);
$p->addSeconds(10);

echo $p->locale('en_US')->forHumans();
echo PHP_EOL;
echo $p->totalSeconds;
// Carbon 3.1.1
//2 minutes 10 seconds
//120.000011

echo $p->locale('en_US')->forHumans();
echo PHP_EOL;
echo $p->totalSeconds;
// Carbon 2.71.0
//2 minutes 10 seconds
//130.000011

After adding additional interval to another one, it not calculated in totalSeconds, but it correct in Human presentation. And it work well at Carbon v2

Carbon version: 3.1.1

PHP version: 8.3

I expected to get:

130

But I actually get:

120

Thanks!

kylekatarnls commented 5 months ago

OK, I see the problem, the interval remember the start and end used to create it, but we need to forget them as soon as the interval is altered again.