Search code examples
datecakephpcakephp-3.0cakephp-3.1

cakephp 3.1 input field with european date format


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.


Solution

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