Reputation: 1765
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.
Upvotes: 0
Views: 2269
Reputation: 1
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
Upvotes: 0
Reputation: 1765
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
Upvotes: 1