cakephp 3.1 input field with european date format

自闭症网瘾萝莉.ら 提交于 2019-12-13 07:03:05

问题


I have a table with records of users (name, forename, birthdate, etc.) and I want to find users by name or birthdate or every possible combination of this.

My code works well, but the birthdate is stored in the american format (YYYY-MM-dd), so I have to search after dates in this format to get the right record. That´s really annoying, because in Germany we use the european format (dd.MM.YYYY) and I want to search for the dates by our habitual format. Now I need help to give the input field the european format and get the right record.

I hope, you understand my issue. :)

Here is an extract of my Controller-Code:

$name = $this->request->data['name'];
$forename = $this->request->data['forename'];
$birthdate = $this->request->data['birthdate'];

if($name || $forename || $birthdate){

    $query = $this->find()
                  ->where([
                     'name LIKE' => '%'.$name.'%',
                     'forename LIKE' => '%'.$forename.'%',
                     'birthdate LIKE' => '%'.$birthdate.'%'
                  ]);

    $number = $query->count();
    $this->set('users', $this->paginate($query));
    $this->set('_serialize', ['users']);

}

And here is the related part of the code from my view:

foreach($users as $user):
    h($user->name)
    h($user->forename)
    h($user->birthdate)
endforeach;

Thank you very much.


回答1:


Your birthdate is stored correctly.

set in bootstrap.php

/**
 * Set the default locale. This controls how dates, number and currency is
 * formatted and sets the default language to use for translations.
 */    
ini_set('intl.default_locale', 'de_DE');

http://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html#setting-the-default-locale

view / search form

<?= $this->From->input('birthdate',['type' => 'text']); ?>

use js / jquery date picker

$('#datepicker').datepicker({ dateFormat: 'dd.mm.yy' }); 


来源:https://stackoverflow.com/questions/34494962/cakephp-3-1-input-field-with-european-date-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!