Welcome Guest, Not a member yet? Register   Sign In
I am not able to insert NOW()
#1

[eluser]shinokada[/eluser]
I have the following model.

Code:
$data = array (
   'customer_id'=> $customer_id,
   'order_date' => NOW(),
   'total' => $totalprice
  );
  $this->db->insert('omc_orders', $data);
  $order_id = $this->db->insert_id();

It does not give any error, but NOW() is inserted as 0000-00-00 00:00:00.

order_date has the type of datetime.

Could anyone tell me what I am doing wrong please?
#2

[eluser]Craig A Rodway[/eluser]
ActiveRecord escapes things like that.

Either use PHP to set the date:

Code:
'order_date' => date('Y-m-d H:i:s'),

Or use the set() function and tell CI not to escape it (but remove the line from your $data array first):

Code:
$this->db->set('order_date', 'NOW()', FALSE);
#3

[eluser]shinokada[/eluser]
Thanks for your help. :-)

The following works.

Code:
$data = array (
   'customer_id'=> $customer_id,
   'total' => $totalprice
  );
  $this->db->set('order_date', 'NOW()', FALSE);
  $this->db->insert('omc_orders', $data);
#4

[eluser]shinokada[/eluser]
Now how can I pull out only date from DATETIME?

SELECT DATE('order_date')
FROM ('omc_orders')

Am I correct?
#5

[eluser]davidbehler[/eluser]
For MySQL have a look at the DATE_FORMAT function.
#6

[eluser]Craig A Rodway[/eluser]
If you need to display it in multiple formats within your app, you can do it this way with a regular select:

Code:
SELECT order_date FROM omc_orders

And just use the PHP date() and strtotime() functions to format and show it, like this:

Code:
echo date('Y-m-d, H:i', strtotime($row->order_date));

(The first parameter can obviously be any valid date format that is accepted by the date() function)




Theme © iAndrew 2016 - Forum software by © MyBB