Wordpress - $wpdb->insert - MySQL NOW() [英] Wordpress - $wpdb->insert - MySQL NOW()

查看:27
本文介绍了Wordpress - $wpdb->insert - MySQL NOW()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在 $wpdb->insert 调用中使用 MySQL NOW()?

is there any possibility to use the MySQL NOW() in the $wpdb->insert call?

当我使用以下代码时,NOW() 不起作用.

When I use the following code, NOW() is not working.

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

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

推荐答案

我相信规范的方法是使用 WordPress current_time() 函数传递它 'mysql' 作为第一个参数来指定一个mysql 时间戳兼容格式(另一种是 UNIX 时间戳格式)和 '1' 作为第二个参数来指定 GMT 时间(默认为本地),如下所示:

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) 输出 2012-07-18 12:51:13.

更多信息:http://codex.wordpress.org/Function_Reference/current_time

这篇关于Wordpress - $wpdb->insert - MySQL NOW()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆