Is there a way to set the default date on the CakePHP date form form?

I have it:

<?php echo $this->Form->input('Schedule.0.end_date', array(
    'minYear' => date('Y'),
    'maxYear' => date('Y')+5
)); ?>

I would like to set the default date to something other than today. Is this possible with the CakePHP form helper?

I found a message that shows how to do this with TIME, but try something like this, setting "day", "month", "year" does nothing.

+3
source share
4 answers

You can achieve this using the parameter selected $this->Form->input();. Try it like this:

<?php
echo $this->Form->input('datetime', array(
  'label' => 'Date 1',
  'selected' => array(
    'day' => '',
    'month' => '',
    'year' => '',
    'hour' => '',
    'minute' => '',
    'second' => ''
    )
  ));
/* What interesting... this will work aswell: */
echo $this->Form->input('datetime', array(
  'label' => 'Date 2',
  'selected' => '0000-00-00 00:00:00'
  ));
?>
+13
source

Just an update for Cake 3. * users: now to precompile the datetime fields you need to use the keyword 'default':

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

"selected" , , , , - - , , .

, , .

:

if (!isset($this->request->data['start_date']))
        $this->request->data['start_date'] = date('Y-m-d', strtotime('-1 month'));

, , , , .

0

CakePHP 2.x,

echo $this->Form->input('end', array(
     'selected' => array(
         'day' => date('d'), 
         'month' => date('m'), 
         'year' => date('Y'), 
         'hour' => date('h'), 
         'min' => date('i'), 
         'meridian' => date('a')
)));

:

$month = DateTime::createFromFormat('!m', date('m'));
$month->format('F');

echo $this->Form->input('end', array(
     'selected' => array(
         'day' => date('d'), 
         'month' => $month->format('F'), 
         'year' => date('Y'), 
         'hour' => date('h'), 
         'min' => date('i'), 
         'meridian' => date('a')
)));
0

All Articles