insert date

#1
[eluser][email protected]_[/eluser]
Hi,

I'm trying to insert the current date into a mysql table and I'm doing something wrong.
Code:
$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!

#2
[eluser]Phil Sturgeon[/eluser]
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:

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.

#3
[eluser]Developer13[/eluser]
Personally I use PHP's time() function as pyro already suggested. Another option is to set your date field data type as [email=http://dev.mysql.com/doc/refman/5.0/en/timestamp.html]TIMESTAMP[/email] and set the default of the field to CURRENT_TIMESTAMP.

#4
[eluser]Techie-Micheal[/eluser]
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.

#5
[eluser][email protected]_[/eluser]
OK, It works. Thank you all! Big Grin

#6
[eluser]winter[/eluser]
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!

#7
[eluser]Phil Sturgeon[/eluser]
Mix the two methods I have used above.

Code:
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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.