Young
Young

Reputation: 8356

Customize the datetime format in Cakephp

When I use the form helper to make a time input,usaually I write code as follows

<?php
$options = array(
    'label' => '',
    'type' => 'datetime',
    'timeFormat'=>'24',
    'separator'=>'-'
);
echo $form->input('Service.endtime',$options);
?>

But now I get a problem that I want to make a time input style such as

month-day-hour

Then how can do this with setting some parameters in the helper?Suggestions would be appreciated.

Upvotes: 1

Views: 15658

Answers (2)

Young
Young

Reputation: 8356

Hope my solution may help someone with the same requirement:

<?php
//echo $form->year('Modelname.year',2001,2021,intval(date('Y')),false)."-";
echo $form->month('Modelname.month',date('m'),'',false)."-";
echo $form->day('Modelname.day',date('d'))."-";
echo $form->hour('Modelname.hour',1,date('H'));
?>

Upvotes: 2

David Yell
David Yell

Reputation: 11855

Well dateformat option will allow you to configure the date parts.

Used to specify the format of the select inputs for a date-related set of inputs. Valid values include ‘DMY’, ‘MDY’, ‘YMD’, and ‘NONE’. http://book.cakephp.org/view/203/options-dateFormat

However for this I think that you will need to extend the helper and create a custom function which will return this mixed date and time style. Otherwise I can only think of having two selects, one with month and day, and one for hour. Then merging the data somewhere else, perhaps in beforeSave() or beforeValidate()

Upvotes: 2

Related Questions