PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 21, 2022

[FIXED] cakephp 3.1 input field with european date format

 February 21, 2022     cakephp, cakephp-3.0, cakephp-3.1, date     No comments   

Issue

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


Answered By - Salines
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing