Saving DATETIME with $wpdb->insert ?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Saving DATETIME with $wpdb->insert ?

BenderisGreat
Having an issue while saving datetime in the db.  Insert is a success with the exception of the datetime field which remains as 0000-00-00 00:00:00.  Not sure why, I am using wordpress' current_time('mysql', 1) format.  Also tried date('Y-m-d h:i:s') without any success.  I feel stupid asking this, but I really dont understand why this is failing. It saves properly if I use %s but I read that datetime should not be saved as a string ever on stackoverflow.  



    $sql2 = $wpdb->insert($activityTable,
    array(
     'user_id' => $profile_id,
     'activity_cat' => 'status',
     'type' => 'status_update',
     'action' => $actionUpdate,
     'content' => $statusUpdate,
     'time' => current_time('mysql', 1)
    ),
    array(
     '%d',
     '%s',
     '%s',
     '%s',
     '%s',
     '%d'
     )
    );
Reply | Threaded
Open this post in threaded view
|

Re: Saving DATETIME with $wpdb->insert ?

John Blackbourn
A date in DATETIME format ("YYYY-MM-DD HH:MM:SS") is a string, not a
number. You need to use %s instead of %d, as you pointed out.

John


On 31 January 2014 01:10, BenderisGreat <[hidden email]> wrote:

> Having an issue while saving datetime in the db.  Insert is a success with
> the exception of the datetime field which remains as 0000-00-00 00:00:00.
> Not sure why, I am using wordpress' current_time('mysql', 1) format.  Also
> tried date('Y-m-d h:i:s') without any success.  I feel stupid asking this,
> but I really dont understand why this is failing. It saves properly if I
> use
> %s but I read that datetime should not be saved as a string ever on
> stackoverflow.
>
>
>
>     $sql2 = $wpdb->insert($activityTable,
>                 array(
>                           'user_id' => $profile_id,
>                           'activity_cat' => 'status',
>                           'type' => 'status_update',
>                           'action' => $actionUpdate,
>                           'content' => $statusUpdate,
>                           'time' => current_time('mysql', 1)
>                 ),
>                 array(
>                           '%d',
>                           '%s',
>                           '%s',
>                           '%s',
>                           '%s',
>                           '%d'
>                           )
>                 );
>
>
>
>
> --
> View this message in context:
> http://wordpress-hackers.1065353.n5.nabble.com/Saving-DATETIME-with-wpdb-insert-tp43278.html
> Sent from the Wordpress Hackers mailing list archive at Nabble.com.
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers