0

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.

Leyla
  • 103
  • 2
  • 11

1 Answers1

0

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' }); 
Salines
  • 5,674
  • 3
  • 25
  • 50
  • I´ll try this. Thank you very much. :) – Leyla Dec 29 '15 at 07:15
  • 1
    I´m still trying to find out how to use the jquery date picker. ;) But yes, I´ll confirm your answer when all works, but I´m confident it will. ;) – Leyla Dec 29 '15 at 07:39