String datetime inserts into mysql database using codeigniter

I have a MySQL table

EventId ObjectKey Title Description When Duration Where Status INT CHAR(36) VARCHAR(500) VARCHAR(8000) DATETIME VARCHAR(500) VARCHAR(500) TINYINT 

and my PHP array

 $data = array( 'Title' => $title, 'Description' => $description, 'When' => $when, 'Duration' => $duration, 'Where' => $where ); 

The $when variable contains 02/21/2013 . When I try to insert into a table using CodeIgniter

 public function insert_event($guid, $data) { $CI = & get_instance(); $CI->load->database('default'); $CI->db->set($data); $CI->db->set('ObjectKey', $guid); $CI->db->set('Status', 1); $vari = $CI->db->insert('Events'); return $vari; } 

Everything is inserted correctly, except for date . Can you help me?

+4
source share
2 answers

Use the correct MYSQL format for the date YYYY-MM-DD . For example, change this in your code:

 $data = array( 'Title' => $title, 'Description' => $description, 'When' => date('Ym-d', strtotime($when)), 'Duration' => $duration, 'Where' => $where ); 
+13
source

Dates in mySQL: YYYY-MM-DD

You insert the date MM/DD/YYYY

So try the following:

 $data = array( 'Title' => $title, 'Description' => $description, 'When' => date('Ym-d', strtotime($when)), 'Duration' => $duration, 'Where' => $where ); 
+4
source

All Articles