2008.12.08

Automagically generated date/time input fields normally default to the current date and time. For a couple of reasons, I had to change this to another default value. For example's sake, let's say I needed a time field to always select 1:30 pm in an add action.

Run of the mill example:

<?php
    
echo $form->input('start_dt');
?>

This will output 3 select boxes; one for hours, minutes, and the merdian (am/pm) with the current time pre-selected. So if it was 3:04 pm, that would be selected.

So lets change this so that 1:30 pm is always pre-selected:

<?php
    
echo $form->input('start_dt', array('selected' => array('hour' => '1',
                                                           
'minute' => '30',
                                                           
'meridian' => 'pm')
                                 )
                );
?>

That's all there is to it! Cake's automagic owns. Hope this helps someone else :)

Get my RSS Feed!

Comments

Josh on (12.16.2008 8:35 pm) says

Thats a great way of doing it, personally I use 'selected' => '01:30:00'

 

Søren on (2.4.2009 6:49 pm) says

I had to use the same way as Josh is

 

Frans on (7.29.2009 3:25 pm) says

This does the trick, hope it helps...

echo $form->input('PlayerProfile.date_of_birth',array('dateFormat'=>'YMD','timeFormat'=>'NONE','selected'=>strtotime('2000-01-01'),'minYear'=> date('Y') - 100,'maxYear' => date('Y')));

 

Chris on (9.27.2009 5:11 am) says

Very helpful.

Just wondering if there's a way to use default values for the values not supplied in the 'select' attribute.

I was hoping to set the time to a specific time, but keep the date as the current date.

Thanks!

 

Ryan on (10.14.2009 5:28 pm) says

Is there a way to just set the year part and leave everything else as the default?  Also, how would you set it to be current year minus 1?  Basically I want the default value to be exactly one year ago today (so the date and time set to the current date and time except the year field set to current year minus 1).

 
* Name
* Email (Will not be displayed)
Website