EllisLab text mark
Advanced Search
     
insert date
Posted: 30 December 2007 06:14 PM
Avatar
Joined: 2007-09-15
12 posts

Hi,

I’m trying to insert the current date into a mysql table and I’m doing something wrong.

$input = array(
    
'title'            => $title,
    
'description'             => $desc,
    
'tag'             => $tag,
    
'attachment'            => $file,
    
'max'            => $max,
    
'date'            => 'NOW()'
    
);
$this->db->insert('some_table',$input); 

The other variables are well inserted. The date field is “datetime” and remains null. What am I missing? Thanks!

 
Posted: 30 December 2007 09:56 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-11
2987 posts

Had a similar problem myself, it will try to treat that entry like it is a string - meaning it will put data = “NOW()” instead of date = NOW().

Use the code:

$input = array(
    
'title'            => $title,
    
'description'             => $desc,
    
'tag'             => $tag,
    
'attachment'            => $file,
    
'max'            => $max,
    
'date'            => date('Y-m-d h:m:s')
// or the unix version
    
'date'            => now()
    );
$this->db->insert('some_table',$input); 

For the 2nd option you will need date helper loaded. Or just use the time() function, they do almost the exact same thing except for it can be turned to local timezone or GMT timezone in the CI config.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

 
Posted: 30 December 2007 11:00 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2007-02-25
620 posts

Personally I use PHP’s time() function as pyro already suggested.  Another option is to set your date field data type as .(JavaScript must be enabled to view this email address) and set the default of the field to CURRENT_TIMESTAMP.

 
Posted: 31 December 2007 01:17 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2007-11-15
33 posts

I’ll add to the me too. Storing time() takes less space than datetime.  It is an 11 character int as opposed to 14 character string (IIRC), aside from that, it gives you the flexibility to play around with the time output (or even input, such as strtotime()), but that’s just me.

 
Posted: 31 December 2007 04:30 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-09-15
12 posts

OK, It works. Thank you all! :D

 
Posted: 19 April 2008 03:32 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2008-03-15
21 posts

Sorry to resurect this thread. I’ve been trying to use CI’s now() function or even PHP’s time() function with CI’s ActiveRecord to insert a date into a datetime field in a mysql table. The only way I could get it to work was doing:

$timenow = now();
$this->created = unix_to_human($timenow, TRUE, ‘eu’);
$this->db->insert(‘users’, $this);

This is my first time using ActiveRecord. It’s a bit confusing to me because of the various ways to do the same thing. I guess there might be a problem there. Because a plain SQL insert using NOW() worked fine.

Anyway, if anyone can let me know why I’m having issue, that would be nice. I’ll use the above method for now.

Cheers!

 
Posted: 19 April 2008 05:03 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-11
2987 posts

Mix the two methods I have used above.

date('Y-m-d h:m:s'now()); 

It might be worth hacking standard_date to add a new entry in the array ‘MYSQL_DATE’ or something like that. Thats the other way I do it on projects where I dont mind extending helpers.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management