PHPackages                             atymic/laravel-dateinterval-cast - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. atymic/laravel-dateinterval-cast

ActiveLibrary[Database &amp; ORM](/categories/database)

atymic/laravel-dateinterval-cast
================================

1.1.0(5y ago)125056[1 issues](https://github.com/atymic/laravel-dateinterval-cast/issues)[2 PRs](https://github.com/atymic/laravel-dateinterval-cast/pulls)MITPHPPHP ^7.3CI failing

Since Feb 27Pushed 5y ago1 watchersCompare

[ Source](https://github.com/atymic/laravel-dateinterval-cast)[ Packagist](https://packagist.org/packages/atymic/laravel-dateinterval-cast)[ Docs](https://github.com/atymic/laravel-dateinterval-cast)[ GitHub Sponsors](https://github.com/atymic)[ RSS](/packages/atymic-laravel-dateinterval-cast/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (4)Versions (4)Used By (0)

Laravel DateInterval / CarbonInterval Cast
==========================================

[](#laravel-dateinterval--carboninterval-cast)

[![Build Status](https://camo.githubusercontent.com/a4bdcef73282f41f82d4861b710ce3c895cd2b84320d7f2b502340454ffaf3ea/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6174796d69632f6c61726176656c2d64617465696e74657276616c2d636173742f5048503f7374796c653d666c61742d737175617265)](https://github.com/atymic/laravel-dateinterval-cast/actions)[![StyleCI](https://camo.githubusercontent.com/7aa27ccaa105383247639c3b199f528fb7e3eb7459cca4722cd4313c8f83e79d/68747470733a2f2f7374796c6563692e696f2f7265706f732f3234333138313937372f736869656c64)](https://styleci.io/repos/243181977)[![Latest Version on Packagist](https://camo.githubusercontent.com/43e5b759c974f29793628b95e3aee5e2d3c14282092496f20df78c7f2ed60430/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6174796d69632f6c61726176656c2d64617465696e74657276616c2d636173742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/atymic/laravel-dateinterval-cast)[![Total Downloads](https://camo.githubusercontent.com/5b21918e717a1593186cf6b3f204b5626460f60330060a0586a8595c1cd35e8c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6174796d69632f6c61726176656c2d64617465696e74657276616c2d636173742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/atymic/laravel-dateinterval-cast)

Laravel has built-in casting for `date` &amp; `datetime` types, but if you want to use ISO 8061 durations with the native `DateInterval` class, or Carbon's `CarbonInterval` you're out of luck.

This package provides two custom casts (for `DateInterval` and `CarbonInterval` respectively) using Laravel 7.x/8.x's custom casts feature.

Installation
------------

[](#installation)

```
composer require atymic/laravel-dateinterval-cast
```

Using this package
------------------

[](#using-this-package)

In your model's `$casts`, assign the property you wish to enable casting on to either of the casts provided by the package. You should use a `varchar`/`string` field in your database table.

```
class TestModel extends Model
{
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'is_xyz' => 'boolean',
        'date_interval' => DateIntervalCast::class,
        'carbon_interval' => CarbonIntervalCast::class,
    ];
}
```

The property on the model will then be cast to an interval object, and saved to the database as a ISO 8061 duration string. If you try to assign an invalid duration (or the database table contains one, and you use a getter) an exception is thrown.

```
$model = new TestModel();

$model->carbon_interval = now()->subHours(3)->diffAsCarbonInterval();

$model->save(); // Saved as `P3H`
$model->fresh();

$model->carbon_interval; // Instance of `CarbonInterval`
$model->carbon_interval->forHumans(); // prints '3 hours ago'

try {
    $model->carbon_interval = 'not_a_iso_period';
} catch (\Atymic\DateIntervalCast\Exception\InvalidIsoDuration $e) {
    // Exception thrown if you try to assign an invalid duration
}
```

Contributing
------------

[](#contributing)

Contributions welcome :) Please create a PR and i'll review/merge it.

Licence
-------

[](#licence)

MIT

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~97 days

Total

3

Last Release

2070d ago

Major Versions

0.1.0 → 1.0.02020-03-03

### Community

Maintainers

![](https://www.gravatar.com/avatar/65eb3a7ba2a2c13b3a9de48b836caf759ad4052f9a839e30464c80d177d5b3d2?d=identicon)[atymic](/maintainers/atymic)

---

Top Contributors

[![atymic](https://avatars.githubusercontent.com/u/50683531?v=4)](https://github.com/atymic "atymic (8 commits)")

---

Tags

casteloquentlaravellaravel-7laravel-frameworklaravel-packagephpphp-librarylaraveleloquentdateintervalcast

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/atymic-laravel-dateinterval-cast/health.svg)

```
[![Health](https://phpackages.com/badges/atymic-laravel-dateinterval-cast/health.svg)](https://phpackages.com/packages/atymic-laravel-dateinterval-cast)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[bavix/laravel-clickhouse

Eloquent model for ClickHouse

72214.1k2](/packages/bavix-laravel-clickhouse)[io238/laravel-iso-countries

Ready-to-use Laravel models and relations for country (ISO 3166), language (ISO 639-1), and currency (ISO 4217) information with multi-language support.

5462.3k](/packages/io238-laravel-iso-countries)[rennokki/eloquent-settings

Eloquent Settings allows you to bind key-value pairs to any Laravel Eloquent model. It supports even casting for boolean, float or integer types.

804.2k](/packages/rennokki-eloquent-settings)[stayallive/laravel-eloquent-observable

Register Eloquent model event listeners just-in-time directly from the model.

2928.9k7](/packages/stayallive-laravel-eloquent-observable)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
