带有欧洲日期格式

带有欧洲日期格式

本文介绍了cakephp 3.1输入字段,带有欧洲日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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;

非常感谢。

推荐答案

您的生日正确存储。

在bootstrap.php中设置

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');

查看/搜索表单

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

使用js / jquery日期选择器

use js / jquery date picker

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

这篇关于cakephp 3.1输入字段,带有欧洲日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:44