Skazmierz Ogrodnik
Skazmierz Ogrodnik

Reputation: 147

Cakephp input dateFormat

I am using CakePHP 2.6.x to create a simple input for type date:-

echo $this->Form->input('datetest', array(
    'label' => false, 
    'type' => 'datetime',
    'dateFormat' => 'YMD', 
    'minYear' => date('Y') - 70, 
    'maxYear' => date('Y') + 10, 
    'selected' => '2012-02-23')
);

This input returns the date in the format dd.mm.yyyy hh:ii:-

<div class="input datetime">
  <input id="ArticleDatetest" class="datetime" type="text" value="23.02.2012 00:00" autocomplete="off" maxyear="2025" minyear="1945" name="data[Article][datetest]">
</div>

The expected date format is yyyy-mm-dd hh:ii. What I am doing wrong?

Upvotes: 2

Views: 7048

Answers (2)

massimoi
massimoi

Reputation: 384

This works very well in CakePHP 3:

<?pho echo $this->Form->text('proposal_date', ['type'=>'date', 'value'=>$project->proposal_date->i18nFormat('yyyy-MM-dd')]); ?>

or you can use the solution https://stackoverflow.com/a/35895954/3813117 which is:

<?
$this->Form->templates(
  ['dateWidget' => '{{day}}{{month}}{{year}}']
);
  echo $this->Form->input('approval_date', ['type' => 'date']); ?>

It uses the native date selector of the browser. Works with Firefox, Chrome, Edge.

Upvotes: 1

Salines
Salines

Reputation: 5769

First, change 'type' => 'datetime' to 'type' => 'date',

Second, you use browser html5 native date picker, wich show you your system datetime format. if you submit form data on your controller data will be in 2015-06-22 format.

Try here

But, this input type is not suported by IE , Firefox, etc.

Solution, use JS date picker and change 'type' => 'text'

Upvotes: 1

Related Questions