How To Input A Date In The Right Format In The Database?

Hi ,

I am following the tutorial which say to have this code in the model file:




    public function behaviors()

{

    return [

        TimestampBehavior::className(),

    ];

}




This code works as it inputs something in the created_at and updated_at column but the input is 0000-00-00

and also, how to validate date? At the moment I have this: [[‘created_at’, ‘updated_at’], ‘integer’], but on updates when the 0000-00-00 comes back it causes problems.

Any idea how I can solve this problem? Don’t forget I am very new to Yii.

Thanks,

Ben

That’s because you may be storing it in the database not as INTEGER but as a DATETIME or TIMESTAMP field (assuming you are using MYSQL). You need to convert date/time formats for display and saving as per your view and database needs.

Alternatively, you can use my yii2-datecontrol extension, which will allow you to set a separate date format globally for saving to model and separate format for display/view. It allows you to override this for each input when rendering at view level as well.

Hi Kartik V,

And thank you for your help.

You were right, the database fields were set to "DATE", I have now set them up as "INT" and get a value, but, the value seems to be showing in a Linux as it shows as "1399621641" in both fields.

Without using extensions(as I would like to understand the things without add ons), is there a way to change the format directly from the above code I posted at the start of the thread please?

I took that code from this page:

http://www.yiiframework.com/doc-2.0/yii-behaviors-timestampbehavior.html

Thank you,

Ben

Hi,

If I understand you well, you can do the following; in your controller action:




   $model->Created = date('y-m-d h:i:s', time()); //it will take your system time and save 

                                                  // it in specified format



Furthermore, if you want to display it in different format, I can give you some tips.

The steps are the following:

  1. Before you display a date on your view (form, grid, etc.) convert the date to a format in which you want to display to users. You can either do it in a controller or a model filter/behavior, or write a global class/function (something similar to below):



   function getViewDate($date, $format = "d-m-Y h:i:s") {

       $time = strtotime($date);

       return date($format, $time);

   }



  1. Next before saving any date from a view through your model you need to convert it back to a format which your db recognizes:



   function getSaveDate($date, $format = "Y-m-d h:i:s") {

       $time = strtotime($date);

       return date($format, $time);

   }



Of course, you need to enhance the functions above, if you consider translations, locales, other formats, and the limitations of strtotime (since it supports only timestamps - range 1901 to 2038).

Thank you for the reply, what I do not not understand is how to pass $data when it inside a rule




public function rules()

    {

        return [

            [['title', 'data'], 'required'],

            [['data'], 'string'],

            [['created_at', 'updated_at'], 'integer'],

            [['title'], 'string', 'max' => 255]

        ];

    }



I generally store dates in the database as a timestamp, so simply use something like this in my model class:




	public function afterFind()

	{

		$this->date = date('m-d-Y', $this->date);


		return parent::afterFind();

	}

 

	public function beforeValidate()

	{

		$this->date = strtotime($this->date);

 

		return parent::beforeValidate();

	}