How to insert NOW() in DATETIME field |
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() { And also with literal 'NOW()' here: PHP Code: $now = 'NOW()'; None of them works.
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
Here is a helper method I wrote to do just that.
PHP Code: // ----------------------------------------------------------------------- What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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()'); -joho
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). What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
You can also use the Time Class.
CodeIgniter 4 User Guide - Times and Dates - now() PHP Code: <?php What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(03-11-2024, 11:37 PM)InsiteFX Wrote: You can also use the Time Class. I can, but won't that also use the server time from where PHP is running, rather than where MySQL is running? ? -joho
No, you can specify what time to use.
PHP Code: $myTime = new Time('now', 'America/Chicago', 'en_US'); What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |