cakephp 3.1输入字段,带有欧洲日期格式

时间:2015-12-28 14:01:44

标签: date cakephp cakephp-3.0 cakephp-3.1

我有一个包含用户记录的表(姓名,姓名,出生日期等),我希望按名称或生日或每种可能的组合找到用户。

我的代码运作良好,但生日日期以美国格式(YYYY-MM-dd)存储,因此我必须在此格式的日期后搜索才能获得正确的记录。这真的很烦人,因为在德国我们使用欧洲格式(dd.MM.YYYY),我想通过习惯格式搜索日期。现在我需要帮助来为输入字段提供欧洲格式并获得正确的记录。

我希望,你了解我的问题。 :)

以下是我的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']);

}

以下是我认为代码的相关部分:

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

非常感谢。

1 个答案:

答案 0 :(得分:0)

您的出生日期存储正确。

在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

查看/搜索表单

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

使用js / jquery日期选择器

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