WordPress insert NOW() in TIMESTAMP column returns all zeros

I want to insert a TIMESTAMP into my column’s TIMESTAMP column, but I always get all zeros.

Here is my insert:

$now = 'NOW()';

// insert the date into the db
 $wpdb->insert( 
    'wp_date', 
    array(
        'name' => $name,
        'date' => $now    
    ), 
    array( 
        '%s',
        '%s'
    ) 
); // end insert

2 Answers
2

I hadn’t tried to reproduce it, but the likely reason is that what you want to use in query is SQL function (NOW()) but what you “tell” WordPress to use is a string.

Declaring its format to be %s means it is sanitized and put in quotes, making it something like 'NOW()' which MySQL probably sees as very invalid timestamp.

You will have to either generate raw SQL query for this (without using insert helper) or generate and provide timestamp string PHP-side.

Leave a Comment