PHPackages                             d3yii2/yii2-datetime - 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. d3yii2/yii2-datetime

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

d3yii2/yii2-datetime
====================

Yii2 extension that adds helpers and behaviors for working with date and time

1.1.1(9y ago)0263PHPPHP &gt;=5.4.0

Since Mar 1Pushed 6y agoCompare

[ Source](https://github.com/d3yii2/yii2-datetime)[ Packagist](https://packagist.org/packages/d3yii2/yii2-datetime)[ RSS](/packages/d3yii2-yii2-datetime/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

Date and Time behaviors for Yii 2
=================================

[](#date-and-time-behaviors-for-yii-2)

This extension helps you to deal with date and time attributes of the models for Yii framework 2.0

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist omnilight/yii2-datetime "*"

```

or add

```
"omnilight/yii2-datetime": "*"
```

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

Why?
----

[](#why)

When working with models and forms it is often needed to provide user a way to edit attributes that holds date and/or time. In this case typical problems are:

1. Date/time formats could be different in the database and in the displayed form (due to local settings, for ex.)
2. You should validate values entered by the user

So you have to set correct format for database, form, somehow convert them from one to another, and it would be nice if you can setup correct formats once (on application level) and lately do not worry about them. This extension helps is this area.

The idea
--------

[](#the-idea)

Idea is to have separate attribute for each editable date/time property of the model, that will be used in the form. This attribute will not be stored in the DB, but it will be used to present user with correctly formatted value. And when you assign value to this attribute, it will be automatically converted and assigned to the DB property of the model.

This extension provides special behavior, that automates work with this attributes

How to use
----------

[](#how-to-use)

In your model:

```
/**
 * @property string posted_at This is your property that you have in the database table, it has DATETIME format
 */
class Post extends ActiveRecord
{
    // ... Some code here

    public function behaviors()
    {
        return [
            'datetime' => [
                'class' => DateTimeBehavior::className(), // Our behavior
                'attributes' => [
                    'posted_at', // List all editable date/time attributes
                ],
            ]
        ];
    }
}
```

Now in your view with the form:

```
// $model has instance of Post
