- # app/views/elements/form/input_datetime.ctp
- # This is a reusable bit to display the date/time inputs.
- <?php $model = isset( $model ) ? $model : $this->model ?>
- <div class="input datetime<?php echo isset( $required ) && $required ? ' required' : '' ?>">
- <?php echo $this->Form->input( $model . '.' . $field . '.date' , array( 'type' => 'text', 'label' => $label, 'value' => !empty( $value ) ? date( DATE_FORMAT_LONG, strtotime( $value ) ) : '', 'div' => false, 'class' => 'date' ) ) ?>
- <?php echo $this->Form->input( $model . '.' . $field, array( 'type' => 'time', 'label' => false, 'div' => false, 'class' => 'time', 'empty' => true ) ) ?>
- </div>
- # app/views/<wherever>/<whatever.ctp>
- # The following code should go within your form
- <?php echo $this->element( 'form/input_datetime', array( 'field' => 'start_time', 'label' => 'Start Time', 'value' => $this->data['Event']['start_time'], 'required' => true ) ) ?>
- # app/app_model.php
- # The comments on this method pretty much describe what it does
- /**
- * Override Model::deconstruct() in order to use an integrated date
- * value, but multipart time value. The parent method expects both
- * date and time to be segmented, but, if a date picker is used to
- * select the date, then that component is unified.
- *
- * In order to use what's already in place, we'll deconstruct the date
- * portion here and then pass everything to the parent method for
- * reassimilation.
- *
- * @param string $field The name of the field to be deconstructed
- * @param mixed $data An array or object to be deconstructed into a field
- * @return mixed The resulting data that should be assigned to a field
- */
- public function deconstruct( $field, $data ) {
- $type = $this->getColumnType( $field );
- if( in_array( $type, array( 'datetime', 'timestamp' ) ) ) {
- if( isset( $data['date'] ) && !empty( $data['date'] ) ) {
- $date = date( 'U', strtotime( $data['date'] ) );
- if( $date ) {
- $data['month'] = date( 'm', $date );
- $data['day'] = date( 'd', $date );
- $data['year'] = date( 'Y', $date );
- }
- }
- }
- return parent::deconstruct( $field, $data );
- }
- # app/webroot/js/<somewhere>/<something>.js
- # Add this to engage the picker.
- /**
- * Set the datepicker date format, where applicable.
- */
- if( $('.date').length ) {
- $('.date').datepicker({ dateFormat: 'MM d, yy' });
- }
- if( Date.format ) {
- Date.format = 'm/d/yyyy';
- $('.date input')
- .datePicker( { clickInput: true, createButton: false } )
- .val( new Date().asString() );
- }