WordPress – $wpdb->insert – MySQL NOW()

I believe the canonical approach is to use the WordPress current_time() function passing it ‘mysql’ as the first parameter to specify a mysql timestamp compatible format (the alternative is UNIX timestamp format) and ‘1’ as the second parameter to specify GMT time (default is local), like this:

$data = array(
    'id' => NULL,
    'order' => serialize($_POST['data']['Order']),
    'created' => current_time('mysql', 1),
    'user_id' => $current_user->ID
);

$wpdb->insert(ORDERS_TABLE, $data);

current_time('mysql', 1) outputs 2012-07-18 12:51:13.

More here: http://codex.wordpress.org/Function_Reference/current_time

Leave a Comment