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

Best way to transform \DateTime in \Carbon #231

Closed
Unibozu opened this issue Dec 13, 2014 · 11 comments
Closed

Best way to transform \DateTime in \Carbon #231

Unibozu opened this issue Dec 13, 2014 · 11 comments

Comments

@Unibozu
Copy link

Unibozu commented Dec 13, 2014

Hi there,

This is not a proper issue, but more a question : is there a proper way to transform a \DateTime object (often returned by third party library - like Doctrine in my case) to a Carbon powered object ?
As the constructor accepts the same arguments as \DateTime it is possible to format the \DateTime to a unambiguous string and send it to Carbon but it doesn't feel nice.

Keep up the good work

@briannesbitt
Copy link
Owner

Right from the docs....

Finally, if you find yourself inheriting a \DateTime instance from another library, fear not! You can create a Carbon instance via a friendly instance() function.

$dt = new \DateTime('first day of January 2008');   // <== instance from another API
$carbon = Carbon::instance($dt);
echo get_class($carbon);                               // 'Carbon\Carbon'
echo $carbon->toDateTimeString();                      // 2008-01-01 00:00:00

Definitely makes me think we are in need of a separate website with better navigation !

@dinamic
Copy link

dinamic commented Apr 9, 2019

It's unfortunate that Carbon::instance() does not make use of \DateTimeInterface. My use case involves using \DateTimeImmutable, which is not well supported atm.

My workaround:

new Carbon($dateTime->format('Y-m-d H:i:s.u'), $dateTime->getTimezone())

Repository owner deleted a comment from Unibozu Apr 9, 2019
@kylekatarnls
Copy link
Collaborator

kylekatarnls commented Apr 9, 2019

As you can see: Carbon::instance(new DateTimeImmutable()) is not a problem. It's supported since Carbon 1.26.

http://try-carbon.herokuapp.com/?embed&theme=xcode&border=silver&options-color=rgba(120,120,120,0.5)&input=%24date%20%3D%20new%20DateTimeImmutable()%3B%0A%0Aecho%20Carbon%3A%3Ainstance(%24date)%3B%0A

Carbon::instance() takes DateTimeInterface as parameter. You can create Carbon/CarbonImmutable from both DateTime/DateTimeImmutable using instance method. And as DateTimeInterface cannot be implemented by the user (PHP restriction) it can only be DateTime/DateTimeImmutable or a class that extend one or the other.

If you still have trouble please fill a complete issue following the template so we'll have all informations we need to help you.

@dinamic
Copy link

dinamic commented Apr 9, 2019

@kylekatarnls thanks for explaining. Turned out we are running 1.22. We'll get to revise on what's stopping us from migrating to a newer version.

@kylekatarnls
Copy link
Collaborator

New features require minor update in semantic version, that's why you should have ^1.x or ^2.x and should run composer update from time to time (for example when your own library/product has a minor release). Also, Carbon 1 was mostly still supported because of Laravel requirement, as Laravel stopped to support its version 5.7 (the last one that used Carbon 1), we'll progressively stop the maintenance of the version 1. The last minor release of Carbon 1 should come before the end of 2019, so prefer to upgrade directly to ^2, it's not a big step, it's 99% backward compatible, breaking changes are listed here: https://carbon.nesbot.com/docs/#api-carbon-2

@kylekatarnls
Copy link
Collaborator

The cleanest way is given in the very first example of the first chapter after intro in docs:
https://carbon.nesbot.com/docs/#api-instantiation

$carbon = new Carbon($dateTime);

It can't be more neat and I don't think we should add more alias for this. Also making all the features being guessable for preferences of everybody without having to read the doc isn't a challenge I would plan to try.

Thanks.

@TomasVotruba
Copy link

I've found this issue when I wanted to migrate DateTime to Carbon to fix our tests. In case anyone is looking for similar solution, we've just added Rector set to handle the migration: https://getrector.com/blog/migrate-datetime-to-carbon

@kylekatarnls
Copy link
Collaborator

kylekatarnls commented May 18, 2024

Thank you @TomasVotruba! I think it can be pretty handy, mainly for people looking for a way to mock time in their tests for their existing code :)

I suggest to also include the conversion for immutable dates:
rectorphp/rector-src#5894

@TomasVotruba
Copy link

TomasVotruba commented May 19, 2024

@kylekatarnls Makes sense, thank you 👍 :)
If you find any other case, let me know. We're still exploring this area and look for cases we've missed.

@kylekatarnls
Copy link
Collaborator

Yep, I think I can suggest few things and might try to get PR for them. I see you have date() to Carbon::now()->format() conversion. In the same way (assuming people are looking for a way to mock time) then we can add time() to Carbon::now()->getTimestamp() (or Carbon::now()->timestamp)

@TomasVotruba
Copy link

Sounds great 👍

then we can add time() to Carbon::now()->getTimestamp() (or Carbon::now()->timestamp)

I thought of that but wasn't sure about best practice. PR would be awesome 🙏

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

5 participants