{"id":15019903,"url":"https://github.com/atymic/laravel-dateinterval-cast","last_synced_at":"2025-10-24T18:30:18.069Z","repository":{"id":54172683,"uuid":"243181977","full_name":"atymic/laravel-dateinterval-cast","owner":"atymic","description":"DateInterval / CarbonInterval custom eloquent cast for Laravel 7.x+","archived":false,"fork":false,"pushed_at":"2021-03-05T13:19:42.000Z","size":29,"stargazers_count":12,"open_issues_count":3,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-31T03:32:43.256Z","etag":null,"topics":["cast","eloquent","laravel","laravel-7","laravel-framework","laravel-package","php","php-library"],"latest_commit_sha":null,"homepage":"https://atymic.dev","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/atymic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-26T06:05:47.000Z","updated_at":"2024-04-16T07:17:10.000Z","dependencies_parsed_at":"2022-08-13T08:20:22.826Z","dependency_job_id":null,"html_url":"https://github.com/atymic/laravel-dateinterval-cast","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atymic%2Flaravel-dateinterval-cast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atymic%2Flaravel-dateinterval-cast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atymic%2Flaravel-dateinterval-cast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atymic%2Flaravel-dateinterval-cast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/atymic","download_url":"https://codeload.github.com/atymic/laravel-dateinterval-cast/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238016147,"owners_count":19402519,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cast","eloquent","laravel","laravel-7","laravel-framework","laravel-package","php","php-library"],"created_at":"2024-09-24T19:54:17.883Z","updated_at":"2025-10-24T18:30:17.748Z","avatar_url":"https://github.com/atymic.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel DateInterval / CarbonInterval Cast\n\n[![Build Status](https://img.shields.io/github/workflow/status/atymic/laravel-dateinterval-cast/PHP?style=flat-square)](https://github.com/atymic/laravel-dateinterval-cast/actions) \n[![StyleCI](https://styleci.io/repos/243181977/shield)](https://styleci.io/repos/243181977) \n[![Latest Version on Packagist](https://img.shields.io/packagist/v/atymic/laravel-dateinterval-cast.svg?style=flat-square)](https://packagist.org/packages/atymic/laravel-dateinterval-cast) \n[![Total Downloads](https://img.shields.io/packagist/dt/atymic/laravel-dateinterval-cast.svg?style=flat-square)](https://packagist.org/packages/atymic/laravel-dateinterval-cast) \n\nLaravel has built-in casting for `date` \u0026 `datetime` types, but if you want to use ISO 8061 durations with the native\n`DateInterval` class, or Carbon's `CarbonInterval` you're out of luck.\n\nThis package provides two custom casts (for `DateInterval` and `CarbonInterval` respectively) using Laravel 7.x/8.x's custom\ncasts feature.\n\n## Installation\n\n```bash\ncomposer require atymic/laravel-dateinterval-cast\n```\n\n## Using this package\n\nIn your model's `$casts`, assign the property you wish to enable casting on to either of the casts provided by the package.\nYou should use a `varchar`/`string` field in your database table.\n\n```php\nclass TestModel extends Model\n{\n    /**\n     * The attributes that should be cast to native types.\n     *\n     * @var array\n     */\n    protected $casts = [\n        'is_xyz' =\u003e 'boolean',\n        'date_interval' =\u003e DateIntervalCast::class,\n        'carbon_interval' =\u003e CarbonIntervalCast::class,\n    ];\n}\n```\n\nThe property on the model will then be cast to an interval object, and saved to the database as a ISO 8061 duration string.\nIf you try to assign an invalid duration (or the database table contains one, and you use a getter) an exception is thrown.\n\n\n```php\n$model = new TestModel();\n\n$model-\u003ecarbon_interval = now()-\u003esubHours(3)-\u003ediffAsCarbonInterval();\n\n$model-\u003esave(); // Saved as `P3H`\n$model-\u003efresh();\n\n$model-\u003ecarbon_interval; // Instance of `CarbonInterval`\n$model-\u003ecarbon_interval-\u003eforHumans(); // prints '3 hours ago'\n\ntry {\n    $model-\u003ecarbon_interval = 'not_a_iso_period'; \n} catch (\\Atymic\\DateIntervalCast\\Exception\\InvalidIsoDuration $e) {\n    // Exception thrown if you try to assign an invalid duration\n}\n```\n\n## Contributing\n\nContributions welcome :) \nPlease create a PR and i'll review/merge it. \n\n\n## Licence\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatymic%2Flaravel-dateinterval-cast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fatymic%2Flaravel-dateinterval-cast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatymic%2Flaravel-dateinterval-cast/lists"}