0

I need to save current date and I supposed I must modify this code into application\libraries\grocery_crud.php

line # 253

case 'date':
/*if(!empty($value) && $value != '0000-00-00' && $value != '1970-01-01')
{
list($year,$month,$day) = explode("-",$value);


$value = date($this->php_date_format, mktime (0, 0, 0, (int)$month , (int)$day , (int)$year));
}
else 
{
$value = '';
}*/


$value = // some code with current date 

It's in that way? or maybe there is another solution, hope can help me, thanks in advance!

Maru
  • 55
  • 3
  • 14
  • you can set default value as current date for db field. so you don't want to send current date from your php code. – Kumar V Jan 25 '14 at 17:41
  • thanks for answer Kumar! I tried but I have this error. ALTER TABLE `cobranzas` CHANGE `fecha_pago` `fecha_pago` DATE ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP MySQL ha dicho: Documentación #1067 - Invalid default value for 'fecha_pago' – Maru Jan 25 '14 at 18:26
  • Actually instead of doing in code, you can set default value in db. So you don't want to pass data. If you pass data, then you will get this error as your code may not work properly. It is not an answer. Just suggestion. – Kumar V Jan 25 '14 at 18:27
  • check this link : http://prajapatinilesh.wordpress.com/2007/10/11/is-it-possible-to-set-current-datetime-as-default-value-in-date-column-in-mysql/ and this also http://stackoverflow.com/questions/13063980/mysql-default-datetime-through-phpmyadmin – Kumar V Jan 25 '14 at 18:51
  • this erros appears when I add "current datetime" in attrib into my table :/ – Maru Jan 25 '14 at 19:43
  • thank u so much for you help, here is the solution: The only field you can use current_time on is a timestamp. – Maru Jan 25 '14 at 20:49

1 Answers1

0

Sometimes you may not be able to change the column type from datetime to timestamp.

On these scenarios, considering a GroceryCrud application, there are two possible (and very easy) solutions:

1) Use callback_insert, create your custom insert logic and set the value for the datetime column using a function like NOW() or GETDATE() depending on the DB your are using: $this->db->set('dateTimeColumnName', 'NOW()', FALSE);

2) You can use the callback_before_insert, and inside it, you can call the function date() from PHP, and set there the current date for your insert in the respective array or object attribute that will be passed / returned for the auto-insert be processed right after the callback´s execution