Question

I have an input that accept a data in this format mm/dd/yyyy.

public function rules()
{
    return array(
        array('purchase_date', 'type', 'type' => 'date', 'message' => '{attribute}: is not a date, try this formate -> (mm/dd/yyyy) !', 'dateFormat' => 'MM/dd/yyyy'),

);
}

however, the question is, how do I change the format to YYYY-MM-DD after I capture it from the input in order to put it into the database with this format YYYY-MM-DD.

example... user input the date in mm/dd/yyyy I somehow convert it to YYYY-MM-DD before I save the model.

Was it helpful?

Solution

Use beforeSave function in your model and add logic to convert date format

public function beforeSave(){

    $this->purchase_date = date("Y-m-d", strtotime($this->purchase_date));
    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top