• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to insert NOW() in DATETIME field

#1
I'm building a seeder to populate the database, but I'm not able to set NOW() in DATETIME fields. How can I do it?

I tried this:
PHP Code:
    public function run() {
        helper('date');
        $now now();
        
        $data 
= [
            [
                'user_id' => 1,
                'group_id' => 1,
                'created_at' => $now,
                'updated_at' => $now
            
],
            [
                'user_id' => 1,
                'group_id' => 2,
                'created_at' => $now,
                'updated_at' => $now
            
],
            [
                'user_id' => 2,
                'group_id' => 2,
                'created_at' => $now,
                'updated_at' => $now
            
]
        ];

        $this->db->table('back_user_groups_rel')->insertBatch($data);
    

And also with literal 'NOW()' here:
PHP Code:
$now 'NOW()'

None of them works.
Reply

#2
now() return int, datetime field is string
So
Code:
$now = date('Y-m-d H:i:s', now());

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply

#3
Thank you!
Reply

#4
Here is a helper method I wrote to do just that.

PHP Code:
// -----------------------------------------------------------------------

/**
 * Method for MySQLi NOW()
 */
if ( ! function_exists('now'))
{
    
/**
     * -------------------------------------------------------------------
     * now ()
     * -------------------------------------------------------------------
     *
     * @return string
     */
    
function now() : string
    
{
        return 
date("Y-m-d H:i:s");
    }

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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