Automate Date generation in yii2

Dan OngudiDan Ongudi
1 min read

To automate the generation of the creation date in your model, follow these steps:

  1. Go to the controller that contains the create function.

  2. Add the following line of code to set the company_created_date attribute with the current date and time:

$model->company_created_date = date('Y-m-d h:i:s');
  1. The entire code for the create function should look like this:
if ($model->load($this->request->post())) {
    $model->company_created_date = date('Y-m-d h:i:s');
    $model->save();
    return $this->redirect(['view', 'company_id' => $model->company_id]);
}

This code will automatically set the company_created_date to the current date and time whenever a new record is created.

0
Subscribe to my newsletter

Read articles from Dan Ongudi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dan Ongudi
Dan Ongudi