0

我有一个包含用户记录(姓名、名字、出生日期等)的表,我想按姓名或出生日期或所有可能的组合查找用户。

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

我希望,你明白我的问题。:)

这是我的控制器代码的摘录:

$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;

非常感谢。

4

1 回答 1

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' }); 
于 2015-12-28T21:35:28.683 回答