Setting The Date Format Globally

Hi all, I have been looking for a solution for this problem

Is there a way to set the time format to UK (dd/mm/yy) across the whole app without going through each view and changing each individual date ?

Hi

override the afterFind method and modify the date as you want

do not forget to override beforeValidate method to save the date properly

check also

http://www.yiiframework.com/forum/index.php/topic/41021-problem-searching-by-date-different-format

Thanks for the links and the following worked for me




  protected function afterFind ()

    {

            // convert to display format

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

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

      

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

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


        parent::afterFind ();

    }


    protected function beforeValidate ()

    {

            // convert to storage format

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

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

        

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

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


        return parent::beforeValidate ();

    }




However when i save a model to the database the dates are YYYY DD MM (and the rest of the dates are YYYY MM DD)

which im assuming will cause errors later on

any ideas ?

Dont know if this means anything but when I change the date on a datepicker (bootstrap) it reverts to MM/DD/YY (on the form) but before I change them it shows the correct date

BTW the date for job_complete is set to today’s date in the form using


 $model->date_complete = date('d/m/Y');

in the controller

try this


$this->date_complete = strtotime (str_replace("/", "-",$this->date_complete));

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

works fine in my case and stores the date in properly format in mysql database

  1. I set datepicker using

 'options' => array(

                'dateFormat' => 'dd/mm/yy', // save to db format

            ),



Actually afterfind gives the date to the view based on format d/m/Y (or dd/mm/yy)

so the datepicker has to set in the same format.

beforeValidate takes the dd/mm/yy and converts to Y-m-d just before date saved in database