Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

CakePHP - Changing the Default Value of a Date-Time Input

DZone's Guide to

CakePHP - Changing the Default Value of a Date-Time Input

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

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 :)

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Published at DZone with permission of Mike Bernat, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}