⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Format MySQL Time fields | Edit as dropdown.



JesterRoberts

JesterRoberts
  • profile picture
  • Member

Posted 25 April 2013 - 14:58 PM

I have startTime and endTime columns in my table which are MySQL time type.

I'd like to show them in grocery crud in hh:mm format and possibly have a dropdown list of 30min increments in the editor.

 

Id this possible?


davidoster

davidoster
  • profile picture
  • Member

Posted 26 April 2013 - 07:49 AM

If the fields are set within MySQL as time then Grocery CRUD displays it in hh:mm:ss format so you don't need to do anything.

If you just need to omit the ss (seconds) just use the callbacks (callback_field, callback_column) to change the format.

 

As far as it concerns the dropdown list of 30 mins try to use the field_type function.


JesterRoberts

JesterRoberts
  • profile picture
  • Member

Posted 02 May 2013 - 01:16 AM

Yes I'd like to only display hours and minutes.

The documentation on callbacks is very brief and I'm not exactly sure how to apply it to my problem.

Can you give or point to an example of callbacks being used to change the way a field is displayed?

Thanks.


JesterRoberts

JesterRoberts
  • profile picture
  • Member

Posted 02 May 2013 - 03:21 AM

Ok as far as I can tell this I should be using callbacks something like this but I'm really not sure.

The fields are displayed correctly but times are never updated in the database.

 

    public function program()
    {
        $this->grocery_crud->set_table('program');
        $this->grocery_crud->field_type('startTime','dropdown', array('1' => '00:30',
                                                                        '2' => '01:00',
                                                                        '3' => '01:30',
                                                                        '4' => '02:00'));
        $this->grocery_crud->callback_column('startTime',array($this,'formatTime'));
        $this->grocery_crud->callback_before_insert(array($this,'beforeInsert'));
        $output = $this->grocery_crud->render();


        $this->_example_output($output);        
    }
    public function beforeInsert ($fields)
    {
        $fields['startTime'] = $fields['startTime'] . ":00";
        return $fields; 
    }
    public function formatTime ($value, $row)
    {
        return substr($value, 0, -3);  
    }
 

davidoster

davidoster
  • profile picture
  • Member

Posted 02 May 2013 - 07:18 AM

Date and/or Time are different field types in PHP and not a string. This is the reason that they are not stored in MySQL.

See an example here, http://stackoverflow.com/questions/2767068/adding-30-minutes-to-time-formatted-as-hi-in-php


Paul Savostin

Paul Savostin
  • profile picture
  • Member

Posted 26 June 2015 - 14:58 PM

If the fields are set within MySQL as time then Grocery CRUD displays it in hh:mm:ss format so you don't need to do anything.

If you just need to omit the ss (seconds) just use the callbacks (callback_field, callback_column) to change the format.

 

As far as it concerns the dropdown list of 30 mins try to use the field_type function.

Hi! Where did you see the "time" in GC field_type?