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

Carbon dates created with different TZ are different as objects, but same as strings #3135

Open
ngreimel opened this issue Jan 15, 2025 · 1 comment

Comments

@ngreimel
Copy link

Hello,

I encountered an issue with the following code:

$dateA = Carbon::parse('2025-01-01T00:00:00+01:00');
$dataB = Carbon::parse('2025-01-01T00:00:00+02:00');

echo $dateA == $dateB ? 'Same' : 'Different'; // 'Different'
echo strcmp($dateA, $dateB) === 0 ? 'Same' : 'Different'; // 'Same'

Carbon version: 3.8.4

PHP version: 8.2.22

I expected to get:

DifferentDifferent

But I actually get:

DifferentSame

Without changing the DEFAULT_TO_STRING_FORMAT, Carbon loses context of the TZ. This results in two different dates being considered different when compared as Carbon objects, but equal when comparing as strings.

Thanks!

@kylekatarnls
Copy link
Collaborator

Hi.

Yes, the default stringification does not include the timezone, so you should format them with timezone included if you want a string comparison including the timezone, not use the implicit cast.

Note that you can also change globally the default method used for implicit string, but it's better to just explicitly format at the place you need to convert date to string.

This is expected behavior.

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

No branches or pull requests

2 participants