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

New createFromFormat for CarbonInterval #1918

Closed
xfudox opened this issue Oct 24, 2019 · 2 comments
Closed

New createFromFormat for CarbonInterval #1918

xfudox opened this issue Oct 24, 2019 · 2 comments
Assignees
Labels
enhancement macro candidate Rather than new methods, this could be a macro/mixin
Milestone

Comments

@xfudox
Copy link

xfudox commented Oct 24, 2019

This would be a proposal, not an issue, but i wasn't sure on where to put it.

The proposal is to implement a createFromFormat($format, $time) constructor for CarbonInterval as Carbon object already has, this to better handle MySQL (and others DBMS) time data type or other string representations of time intervals.

It is tedious to use a long

Carboninterval::create(0, 0, 0, 0, 1, 30, 0, 0)

or even a

Carboninterval::hours(1)->minutes(30)

since i have to split the original string into several variables, and eventyally calculate days, weeks and so on.

It would be more readable and user friendly something like

Carboninterval::createFromFormat('H:i', '1:30')
@kylekatarnls kylekatarnls added enhancement macro candidate Rather than new methods, this could be a macro/mixin labels Oct 24, 2019
@kylekatarnls kylekatarnls changed the title New creteFromFormat for CarbonInterval New createFromFormat for CarbonInterval Oct 24, 2019
@kylekatarnls
Copy link
Collaborator

kylekatarnls commented Oct 24, 2019

Hi, this sounds very interesting and pretty simple.

I can already give you a macro that would do exactly that:

CarbonInterval::macro('createFromFormat', static function (string $format, string $interval) {
  return Carbon::createFromFormat('!'.ltrim($format, '!'), $interval, 'UTC')->diffAsCarbonInterval(
    Carbon::parse('1970-01-01 00:00:00', 'UTC')
  );
});

echo Carboninterval::createFromFormat('H:i', '01:30');

If you want to propose it as a core method of CarbonInterval, I would release it in next minor release.

Note: to be strict H should be 01.

@kylekatarnls
Copy link
Collaborator

Will be released in 2.27.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement macro candidate Rather than new methods, this could be a macro/mixin
Projects
None yet
Development

No branches or pull requests

2 participants