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

Period start and end always wrong with Carbon 3 #2982

Closed
bakerkretzmar opened this issue Mar 27, 2024 · 3 comments · Fixed by #2984
Closed

Period start and end always wrong with Carbon 3 #2982

bakerkretzmar opened this issue Mar 27, 2024 · 3 comments · Fixed by #2984
Assignees

Comments

@bakerkretzmar
Copy link

In Carbon 3 the start and end properties of a CarbonPeriod or CarbonPeriodImmutable are always 2000-01-01 and null.

Sandbox example.

Code to reproduce the issue:

[
    Carbon::parse('Sep 1')->toPeriod('Sep 30')->start,
    Carbon::parse('Sep 1')->toPeriod('Sep 30')->end,
];

Carbon version: 3.1.1

PHP version: 8.2.15

Expected (and what Carbon 2 returns):

[
    '2024-09-01 00:00:00', // Carbon\Carbon instance
    '2024-09-30 00:00:00', // Carbon\Carbon instance
];

Received:

[
    '2000-01-01 00:00:00', // DateTime instance
    null,
];
@bakerkretzmar bakerkretzmar changed the title Period start and end always wrong with Carbon 3 Period start and end always wrong with Carbon 3 Mar 27, 2024
@kylekatarnls
Copy link
Collaborator

It's because now CarbonPeriod extends DatePeriod but readonly cannot be controlled from sub-class.

I recommend you use the getters:

[
    Carbon::parse('Sep 1')->toPeriod('Sep 30')->getStartDate(),
    Carbon::parse('Sep 1')->toPeriod('Sep 30')->getEndDate(),
];

@kylekatarnls
Copy link
Collaborator

We can make it correct at creation and then CarbonPeriodImmutable would be compatible including start and end properties, the issue will be that for CarbonPeriod start and end can be changed after creation but the properties would sill contain the old values.

@bakerkretzmar
Copy link
Author

Thanks! 🙏🏻

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

Successfully merging a pull request may close this issue.

2 participants