2

I am developing a Laravel project. I try to create a DateTime object by using Carbon. This is what I tried:

Carbon::createFromFormat('Y-m-d H:i:s', '2021-10-01T00:01:00')->toDateTime();

But my phpstan complains : Cannot call method toDateTime() on Carbon\Carbon|false.

Why is this error? What is the correct way to convert Carbon to a DateTime object?

6
  • 2
    The time you've supplied is not in the format you told Carbon to expect. So createFromFormat() fails, probably returning false. Nov 11, 2021 at 13:55
  • @Don'tPanic but that's something that PHPStan cannot detect
    – Nico Haase
    Nov 11, 2021 at 14:02
  • Y-m-d H:i:s versus 2021-10-01T00:01:00. Your format has a space; your string has a T instead. Your date string doesn't match your expected format.
    – ceejayoz
    Nov 11, 2021 at 14:08
  • @ceejayoz that's something that PHPStan cannot detect through static analysis
    – Nico Haase
    Nov 11, 2021 at 14:08
  • @NicoHaase Well, OP's code as-written throws an exception. They should fix that first, if nothing else.
    – ceejayoz
    Nov 11, 2021 at 14:10

3 Answers 3

5

Your format is incorrect, so Carbon cannot create the time. You're missing the T, which needs to be escaped.

Carbon::createFromFormat('Y-m-d\TH:i:s', '2021-10-01T00:01:00')->toDateTime();
7
  • Please share more details. If PHPStan threw that initial error, what makes you think that your code works different?
    – Nico Haase
    Nov 11, 2021 at 14:02
  • Tried, doesn't help.
    – user842225
    Nov 11, 2021 at 14:03
  • 1
    What about it doesn't help? It works for me. Though as ceejayoz said in his answer, Carbon is already a DateTime object, and can be used as such. Check out the documentation for all of the extra features over DateTime.
    – aynber
    Nov 11, 2021 at 14:04
  • 1
    @NicoHaase Because I tested it out. When I used Carbon\Carbon::createFromFormat('Y-m-d\TH:i:s', '2021-10-01T00:01:00')->toDateTime(); in Tinker, I got a DateTime object with date: 2021-10-01 00:01:00.0 America/New_York (-04:00),. However, I'm not familiar with PHPStan.
    – aynber
    Nov 11, 2021 at 14:05
  • 1
    I don't use PHPStan, so I'm not familiar with it. This only fixes the formatting error.
    – aynber
    Nov 11, 2021 at 14:10
2

If PHPStan complains, that's because the static analysis (which does not execute the code) cannot determine the types properly. As Carbon extends DateTime, the PHP documentation can help for this method call:

Returns a new DateTime instance or false on failure.

So, to ensure that the code is sound in terms of static analysis, you need to split it up:

$object = Carbon::createFromFormat('Y-m-d H:i:s', '2021-10-01T00:01:00');

if (!$object instanceof Carbon) {
  throw new RuntimeException('could not parse date');
}

$object->toDateTime();

The difference: now, PHPStan can safely assume that $object is of type Carbon when toDateTime() is called


As others pointed out: running that code would also yield an error, as the date format you try to parse from and the input date do not match. But that is out of scope for PHPStan, which does not execute the code

0

Carbon objects are already DateTime objects.

class Carbon extends \DateTime

You may want toDateTimeString, but if you really want a DateTime object, you've already got one, just with a little Carbon syntax sugar sprinkled on top.

If you really need a DateTime object, the ->toDate() function is what you're looking for.

https://carbon.nesbot.com/docs/

Return native DateTime PHP object matching the current instance.

2
  • Yep, I know it is a wrapper of DateTime, but I tried what I posted, it yields error. also tried ->toDate(), still get phpstan error: Cannot call method toDate() on Carbon\Carbon|false. Have you tried the code? Does it really work for you?
    – user842225
    Nov 11, 2021 at 14:04
  • @user842225 Well, you've told it to expect Y-m-d H:i:s, but you've provided Y-m-d\TH:i:s formatted data. The text you're giving Carbon doesn't match the format you say it's in.
    – ceejayoz
    Nov 11, 2021 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.