0

In my database, date of births are stored as Y-m-d format, for ex. 1965-09-04. When cakePHP is fetching is from the database, its internally converting these date to d/m/y format, so 1965-09-04 becomes 4/9/65.

Now when I try to convert this date to dd/mm/YY format (04/09/1965), it gives (04/09/2065).

Short story, I don't want cakephp to convert date formats while fetching from DB.

Sourabh
  • 1,757
  • 6
  • 21
  • 43
  • Which Cakephp, 2 or 3?As far as i know, cakephp does not convert date. Do you have some beforeSave in your Model? – gdm Aug 02 '16 at 20:53
  • Its cakePHP 3. No, the date is saving correctly in DB so it can not be beforeSave. Its coming in the view in the wrong format. – Sourabh Aug 02 '16 at 21:05

2 Answers2

1

Found the answer. It was actually formatting date while data was going from controller to the view. Changed default date format of cakephp using bootstrap file. Added this in the file :

Cake\I18n\Date::setToStringFormat('YYYY-MM-dd');
Cake\I18n\FrozenDate::setToStringFormat('YYYY-MM-dd');

\Cake\Database\Type::build('date')
    ->useImmutable()
    ->useLocaleParser()
    ->setLocaleFormat('YYYY-MM-dd');

Reference : Cakephp 3.2 change default date format

Community
  • 1
  • 1
Sourabh
  • 1,757
  • 6
  • 21
  • 43
0

Simply if you want to change date format then according to cakephp rule you can use like this. First change the field datatype int in database then. in the given modal use these function.

public function beforeSave(){

$this->data['TableName']['FieldName']   =   time();
}

public function afterFind($results, $primary = false) {
 foreach ($results as $key => $val) {
        if (isset($val['TableName']['FieldName'])) {
            $results[$key]['TableName']['FieldName'] = $this->dateFormatAfterFind(
                $val['TableName']['FieldName']
            );
        }


    }
    return $results;
}

public function dateFormatAfterFind($dateString) {

    return date("Y-m-d H:m",$dateString);
}

I hope this will for you

krishan
  • 1
  • 2