PHPackages                             jc-it/yii2-datetime-behavior - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. jc-it/yii2-datetime-behavior

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jc-it/yii2-datetime-behavior
============================

Objects of date/time/timestamp fields via magic getter and setter.

v0.1.0(7y ago)03MITPHPPHP &gt;=7.1.0

Since Jun 3Pushed 7y ago1 watchersCompare

[ Source](https://github.com/JC-IT/yii2-datetime-behavior)[ Packagist](https://packagist.org/packages/jc-it/yii2-datetime-behavior)[ RSS](/packages/jc-it-yii2-datetime-behavior/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

DateTime behaviorfor Yii2
=========================

[](#datetime-behaviorfor-yii2)

This extension provides a package to help working with datetime objects in (most often) Active Record objects.

It provides magic getters and setters to work with DateTime objects instead of having to work with strings. It does this with proxies to keep rules (i.e.) working on the standard attributes.

```
$ composer require jc-it/yii2-datetime-behavior
```

or add

```
"jc-it/yii2-datetime-behavior": "^"

```

to the `require` section of your `composer.json` file.

Configuring
-----------

[](#configuring)

In a model:

```
/**
 * @return array
 */
public function behaviors(): array
{
    return ArrayHelper::merge(
        parent::behaviors(),
        [
            DateTimeBehavior::class => [
                'class' => DateTimeBehavior::class
            ],
        ]
    );
}
```

For an Active Record model it will automatically detect date/time/datetime/timestamp fields and will apply the behavior to them.

The full configuration:

```
/**
 * @return array
 */
public function behaviors(): array
{
    return ArrayHelper::merge(
        parent::behaviors(),
        [
            DateTimeBehavior::class => [
                'class' => DateTimeBehavior::class,
                'attributes' => [ // Map of attribute to type.
                    'date_attribute' => DateTimeBehavior::TYPE_DATE,
                    'datetime_attribute' => DateTimeBehavior::TYPE_DATETIME,
                    'time_attribute' => DateTimeBehavior::TYPE_TIME,
                ],
                'attributeSuffix' => 'Object', // Suffix that will be used to detect if the behavior must be triggered.
                'attributeTimezone' => 'UTC', //When storing datetime values in timestamp fields this should default timezone of your database.
                'dateTimeClass' => Carbon::class, //The class of the datetime objects to be returned. Must extend from Carbon.
            ],
        ]
    );
}
```

Usage
-----

[](#usage)

If the attributes that are affected by the behavior are accessed with the suffix an object of the configured class will be returned.

```
// Event has attribute from and until which are timestamps in the database
// Assuming in timezone Europe/Amsterdam

$event = Event::find()->one();
var_dump($event->from);
var_dump($event->fromObject);

// Will output

string '2019-01-01 12:00:00' (length=19)

object(Carbon\Carbon)
  public 'date' => string '2019-01-01 13:00:00.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Amsterdam' (length=16)
```

Credits
-------

[](#credits)

- [Joey Claessen](https://github.com/joester89)

License
-------

[](#license)

The MIT License (MIT). Please see [LICENSE](https://github.com/jc-it/yii2-datetime-behavior/blob/master/LICENSE) for more information.

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

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

Unknown

Total

1

Last Release

2583d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/88359ebe0456645a614e563dfbf0dd025b69ef57221050cb9eb31e3eef4dd0f9?d=identicon)[joester89](/maintainers/joester89)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/jc-it-yii2-datetime-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/jc-it-yii2-datetime-behavior/health.svg)](https://phpackages.com/packages/jc-it-yii2-datetime-behavior)
```

###  Alternatives

[illuminate/support

The Illuminate Support package.

582110.9M40.0k](/packages/illuminate-support)[craftcms/feed-me

Import content from XML, RSS, CSV or JSON feeds into entries, categories, Craft Commerce products, and more.

292943.4k27](/packages/craftcms-feed-me)[solspace/craft-freeform

The most flexible and user-friendly form building plugin!

53675.5k16](/packages/solspace-craft-freeform)[erlandmuchasaj/laravel-gzip

Gzip your responses.

40140.4k2](/packages/erlandmuchasaj-laravel-gzip)[japanese-date/japanese-date

日本の暦、祝日を取り扱うライブラリ

169.9k](/packages/japanese-date-japanese-date)

PHPackages © 2026

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