Display Format in datetime Field

Hi,

in my Events_model I need to display the weekday name next to the datepicker.

I kind of expected it will all happen magically if I set the date_format'D, d.m.Y', to get Mon, 28.08.2019

public function form_fields($values = array(), $related = array())
{

    $fields['start_date']['label']        =  'Datum von';
    $fields['start_date']['date_format']  =  'D, d.m.Y';

    $fields['end_date']['label']          =  'Datum bis';
    $fields['end_date']['date_format']    =  'D, d.m.Y';
}

It's shown correctly in the start_date field but not in the end_date field and saving the field makes problems as well.

Do I need to transform those fields myself, before saving?

I checked the values in function on_before_clean($values) where they appear in the D, d.m.Y format, but in public function on_before_save($values) $values['start_date'] is null. So somehow, something gets lost in between, and I can't make any sense of it.

I appreciate any hint.

Comments

  • I guess I have to prepare and reformat the fields myself. This did it for me:

        function on_before_clean($values)
        {
            $values = parent::on_before_clean($values);
    
            $fields = $this->form_fields();
    
            foreach($fields as $key => $field){
    
                if(isset($field['type']) && $field['type'] == 'datetime' && !empty($field['date_format'])){
    
                    $date = date($field['date_format'], strtotime($values[$key]));
                    $time = sprintf('%02d:%02d', $values[$key. '_hour'], $values[$key. '_min']);
                    $date .= ' '.$time;
                    $values[$key] = date('Y-m-d H:i:s', strtotime($date));
                }
            }
            return $values;
        }
    
    

    If there is a built-in way, let me know. Thanks!

  • That would be the way to do it at this time.

  • Ok, cool! Thanks for confirming.

Sign In or Register to comment.