Convert timestamp to readable date/time

edited October 2014 in Modules
My db have store datetime in (int, 11), now i want to view it when form load/edit to readable datetime.
I used:
$read_datetime = date('d-m-Y',$fields['datetime']);
$fields['datetime'] = array('value' => $read_datetime);

When form edit load, It's still display datetime like '124234772'. So that, I used other way to overwrite form.

In custom_fields.php:
$fields['read_datetime'] = array(
'class' => array(FUEL_FOLDER => 'Fuel_custom_fields'),
'function' => 'real_datetime',
'filepath' => '',
);
In Fuel_custom_fields.php:

public function real_datetime($params)
{
$form_builder =& $params['instance'];
$params['value'] = date('d-m-Y', $value);
$params['type'] = 'text';
return $this->$params;
}
In my model:

function form_fields($values = array(), $related = array())
{
$option_date = $fields['start_time'];
$fields['start_time'] = array('order' => 3, 'type' => 'read_datetime', 'values' => $option_date);
}

But It's not working. Just display datetimepicker.

How could I edit datetime in form view edit to view datetime like mm/dd/yy : hh:mm:ii ?

Thanks

Comments

  • edited October 2014
    Setting the 'value' property that way won't work because it gets overwritten by what is saved in the database. There are 2 ways you can try to fix this. The first is to either overwrite the find_one_array method on your model or create a new method similar to the find_one_array, that will format the date results how you want:
    ... function my_find_one_array($where) { $data = parent::find_one_array($where); foreach($data as $key => $val) { $data[$key]['start_time'] = date('d-m-Y', $val); } return $data; } ...
    Then in your fuel/application/config/MY_fuel_modules.php file, add the edit_method parameter:
    .... $config['modules']['my_module'] = array( ... 'edit_method' => 'my_find_one_array', ... ); ....

    The second method you could also try is adding a 'pre_process' parameter:
    public function real_datetime($params) { $form_builder =& $params['instance']; $func_str = ' return date('d-m-Y', $value); '; $func = create_function('$value', $func_str); $form_builder->set_pre_process($params['key'], $func); return $this->create_datetime($params); }
  • edited 11:34PM
    Hi,
    I'm truly thanks. I'm new at FUEL CMS. 2 way of ur suggest i have try but not success. Would you mind to give me an example. Thanks
  • edited 11:34PM
    I was missing a crucial part in the "my_find_one_array" method above where you have to loop through the data to transform it then return that data. Try that.
  • edited 11:34PM
    Hi, it's still get error when i used the first method.

    A Database Error Occurred

    Error Number: 1054

    Unknown column '12906' in 'where clause'

    SELECT `users`.* FROM (`users`) WHERE `12906` IS NULL LIMIT 1
  • edited 11:34PM
    Actually try this:
    ... function my_find_one_array($id) { $where['id'] = $id; $data = parent::find_one_array($where); foreach($data as $key => $val) { $data[$key]['start_time'] = date('d-m-Y', $val); } return $data; } ...
  • edited 11:34PM
    Thanks. i did it
Sign In or Register to comment.