WordPress: Incorporating MySQL NOW() with $wpdb->insert

I am trying to use MySQL NOW() directly in a $wpdb->insert query within WordPress, but it doesn’t work as intended. See the modified example below:

$newData = [
    'entry_id'  => null,
    'orderInfo' => serialize($_POST['detail']['Items']),
    'timestamp' => NOW(),
    'authorID'  => $currentUser->ID
];

global $dbHandler;
$dbHandler->insert('entries_table', $newData);

hey, try using php’s date(‘Y-m-d H:i:s’) instead of now(). $wpdb->insert just puts the value in as a string so your now() never gets executed realy. hope this helps ya out.

The issue is due to how $wpdb->insert interprets the values passed in the insert array – it treats them as strings, so MySQL functions like NOW() aren’t executed as expected. In my experience, it is better to either set a default value of CURRENT_TIMESTAMP in your table schema or to pass the current date and time from PHP using date(‘Y-m-d H:i:s’), ensuring the timestamp is recorded correctly. This approach avoids any unexpected behavior and leverages the strengths of PHP or the database appropriately.