1
votes

I am trying to make a date input that defaults to the value that is already in the database.

However, when I set the month, year, and date to the database values, the month, date, and year attributes are set on the select element but the page still displays the current date as the default values. When the form is submitted, today's date is stored in the database.

Heres the code:

$mail_date_time = \explode(" ",$campaign["MailedDate"]);
$mail_date = explode("-",$mail_date_time[0]);

echo $this->Form->create("Campaign");
    echo $this->Form->input("MailedDate",array(

        'month' =>strtotime($mail_date[1]), 
        'year' => strtotime($mail_date[0]), 
        'day' => strtotime($mail_date[2])
    ));

echo $this->Form->end("Submit");

$mailed_date turns out to be: [0]=2009 [1]=11 [2]=11

Does anyone know how to solve this? Thanks!

2
Leaving it without any changes in Y-m-d format doesn't work?marian0

2 Answers

0
votes

The Cake form helper includes formatting of dates... i.e. 'dateFormat' => 'DMY' in your MailedDate input.

The value displayed by the input field would default to the value contained in $this->data and can be overwritten completely using 'default' => 'value', or prefilled using 'empty' => 'value'.

0
votes

In order to precompile the datetime fields is necessary to use the 'default' keyword:

$mydate = '2015-09-10 06:40:00'
echo $this->Form->input('datetime', array(
  'label' => 'Date 2',
  'default' => $mydate
));