Wednesday, July 9, 2008

Setting a date or datetime field to current date or time

If you need a datetime or datetime field to reflect the current date and/or time when adding, use the following in your controller:

public function before_add() {

//assuming our datetime field is called 'entry_datetime'
$this->model->set_db_param_type( 'entry_datetime', 'function' ); //tell the model that we don't want a literal 'NOW()' inserted
$this->model->entry_datetime = 'NOW()';


You can also add the same code to before_edit() if you need the same functionality when editing

No comments: