CodeIgniter Forums
How to insert NOW() in DATETIME field - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to insert NOW() in DATETIME field (/showthread.php?tid=77640)



How to insert NOW() in DATETIME field - keop - 09-29-2020

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.


RE: How to insert NOW() in DATETIME field - nc03061981 - 09-29-2020

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



RE: How to insert NOW() in DATETIME field - keop - 09-29-2020

Thank you!


RE: How to insert NOW() in DATETIME field - InsiteFX - 09-29-2020

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");
    }




RE: How to insert NOW() in DATETIME field - joho - 03-06-2024

As has been pointed out in other posts, it should, however, be noted that this is not necessarily the same as the NOW() function in MySQL since that uses the time on the DB-server, and not on the server running PHP.

In some environments, this won't matter, but for others it may.

Perhaps this answer should be updated for CI4 and point in this direction:

Code:
new RawSql('NOW()');



RE: How to insert NOW() in DATETIME field - InsiteFX - 03-06-2024

Here is the full listing of it.

w3resurce - MySQL NOW() function

Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS.uuuuuu (numeric).


RE: How to insert NOW() in DATETIME field - InsiteFX - 03-11-2024

You can also use the Time Class.

CodeIgniter 4 User Guide - Times and Dates - now()

PHP Code:
<?php

use CodeIgniter\I18n\Time;

$now = new Time('now');

    'created_at' => $now,
    'updated_at' => $now



RE: How to insert NOW() in DATETIME field - joho - 03-11-2024

(03-11-2024, 11:37 PM)InsiteFX Wrote: You can also use the Time Class.

CodeIgniter 4 User Guide - Times and Dates - now()

PHP Code:
<?php

use CodeIgniter\I18n\Time;

$now = new Time('now');

    'created_at' => $now,
    'updated_at' => $now

I can, but won't that also use the server time from where PHP is running, rather than where MySQL is running? ?


RE: How to insert NOW() in DATETIME field - InsiteFX - 03-12-2024

No, you can specify what time to use.

PHP Code:
$myTime = new Time('now''America/Chicago''en_US'); 



RE: How to insert NOW() in DATETIME field - joho - 03-12-2024

(03-12-2024, 02:01 AM)InsiteFX Wrote: No, you can specify what time to use.

PHP Code:
$myTime = new Time('now''America/Chicago''en_US'); 

Fair enough, but it requires that I know what timezone the database server is using. We don't need to nitpick this one Cool , I just wanted to point it out Smile