Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CarbonInterval's days property return false #3018

Open
daniser opened this issue May 8, 2024 · 2 comments
Open

CarbonInterval's days property return false #3018

daniser opened this issue May 8, 2024 · 2 comments

Comments

@daniser
Copy link

daniser commented May 8, 2024

Hello,

I encountered an issue with the following code:

echo (new \DateTime('2024-05-08'))->diff(new \DateTime('2024-05-11'))->days;

3

echo (new \Carbon\Carbon('2024-05-08'))->diff(new \Carbon\Carbon('2024-05-11'))->days;

false

Carbon version: 3.3.1

PHP version: 8.3.4

I expected to get:

3

But I actually get:

false

Thanks!

@fxbt
Copy link

fxbt commented May 8, 2024

Before the 3.0, the Carbon::diff method simply returned a \DateInterval but now there is an intermediate CarbonInterval object that is created with an interval_spec string.

As stated in the DateInterval phpdoc

If the DateInterval object was created by DateTimeImmutable::diff() or DateTime::diff(), then this is the total number of full days between the start and end dates. Otherwise, days will be false.

It's a problem for the days property, it's never set and unfortunately it can't be set manually. I tried a lot of things but I don't think there is an easy solution to handle this directly in the vendor

@daniser You can replace all the Carbon::diff in your code with the Carbon::diffAsDateInterval method to keep the same behaviour, or you can use the Carbon::diffInDays method but you'll get a float so don't forget to cast it if you need an integer

@kylekatarnls
Copy link
Collaborator

You can also use ->format('%a') instead of ->days it's got overridden recently to behave the same way it would with a diff DateInterval

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants