Skip to content
PHP Ecosystem
Explore

TimestampBehavior

API reference › yii › behaviors › TimestampBehavior

classyii\behaviors\TimestampBehavior
ExtendsAttributeBehavior » yii\base\Behavior » yii\base\BaseObject
Implementsyii\base\Configurable
Since2.0
Sourceframework/behaviors/TimestampBehavior.php

TimestampBehavior automatically fills the specified attributes with the current timestamp.

To use TimestampBehavior, insert the following code to your ActiveRecord class:

use yii\behaviors\TimestampBehavior;
public function behaviors()
{
return [
TimestampBehavior::class,
];
}

By default, TimestampBehavior will fill the created_at and updated_at attributes with the current timestamp when the associated AR object is being inserted; it will fill the updated_at attribute with the timestamp when the AR object is being updated. The timestamp value is obtained by time().

Because attribute values will be set automatically by this behavior, they are usually not user input and should therefore not be validated, i.e. created_at and updated_at should not appear in the rules() method of the model.

For the above implementation to work with MySQL database, please declare the columns(created_at, updated_at) as int(11) for being UNIX timestamp.

If your attribute names are different or you want to use a different way of calculating the timestamp, you may configure the $createdAtAttribute, $updatedAtAttribute and $value properties like the following:

use yii\db\Expression;
public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
'createdAtAttribute' => 'create_time',
'updatedAtAttribute' => 'update_time',
'value' => new Expression('NOW()'),
],
];
}

In case you use an Expression object as in the example above, the attribute will not hold the timestamp value, but the Expression object itself after the record has been saved. If you need the value from DB afterwards you should call the refresh() method of the record.

TimestampBehavior also provides a method named touch() that allows you to assign the current timestamp to the specified attribute(s) and save them to the database. For example,

$model->touch('creation_time');
PropertyTypeDescriptionDefined by
$attributesarrayList of attributes that are to be automatically filled with the value specified via $value. Default: [].AttributeBehavior
$createdAtAttributestring|falseThe attribute that will receive timestamp value Set this property to false if you do not want to record the creation time. Default: 'created_at'.TimestampBehavior
$owneryii\base\T|nullThe owner of this behavior Default: null.yii\base\Behavior
$preserveNonEmptyValuesboolWhether to preserve non-empty attribute values. Default: false. Available since 2.0.13.AttributeBehavior
$skipUpdateOnCleanboolWhether to skip this behavior when the $owner has not been modified Default: true. Available since 2.0.8.AttributeBehavior
$updatedAtAttributestring|falseThe attribute that will receive timestamp value. Default: 'updated_at'.TimestampBehavior
$valuemixedDefault: null.TimestampBehavior
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
attach()Attaches the behavior object to the component.yii\base\Behavior
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
detach()Detaches the behavior object from the component.yii\base\Behavior
evaluateAttributes()Evaluates the attribute value and assigns it to the current attributes.AttributeBehavior
events()Declares event handlers for the $owner’s events.AttributeBehavior
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.TimestampBehavior
touch()Updates a timestamp attribute to the current timestamp.TimestampBehavior
getValue() protectedReturns the value for the current attributes.TimestampBehavior
public string|false $updatedAtAttribute = 'updated_at'

The attribute that will receive timestamp value.

Set this property to false if you do not want to record the update time.

public mixed $value = null

In case, when the value is null, the result of the PHP function time() will be used as value.

This can be an anonymous function, callable in array format (e.g. [$this, 'methodName']), an Expression object representing a DB expression (e.g. new Expression('NOW()')), scalar, string or an arbitrary value. If the former, the return value of the function will be assigned to the attributes. The signature of the function should be as follows,

function ($event)
{
// return value will be assigned to the attribute
}
public function init(): mixed

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

Returns

mixed

public function touch(string $attribute): mixed

Updates a timestamp attribute to the current timestamp.

$model->touch('lastVisit');

Parameters

ParameterTypeDescription
$attributestringThe name of the attribute to update.

Returns

mixed

Throws

protected function getValue(mixed $event): mixed

Returns the value for the current attributes.

In case, when the $value is null, the result of the PHP function time() will be used as value.

This method is called by evaluateAttributes(). Its return value will be assigned to the attributes corresponding to the triggering event.

Parameters

ParameterTypeDescription
$eventmixedThe event that triggers the current attribute updating.

Returns

mixed: The attribute value