这是获取日期时间的错误方法吗?

时间:2013-02-08 18:34:12

标签: php datetime

我通过这种方式获得日期时间:

class DB_Functions extends DB_Connect{

    private $dbConnect = "";
    public $dtime;

    public function __construct() {
        $this->dbConnect = $this->pdo_connect();
        $this->dtime = new DateTime();
    }

    public function destruct() {
        $this->dbConnect = null;
    }


    public function exampleInsert() {
        .
        .
        .
        $result->bindParam(':dateCreation',$this->dtime->format("Y-m-d H:i:s"),PDO::PARAM_STR);
    }
}

然后当我使用 dtime 插入表格时,如下所示:

Line 1708: $result->bindParam(':dateCreation',$this->dtime->format("Y-m-d H:i:s"),PDO::PARAM_STR);

显示此错误:

<b>Strict Standards</b>:  Only variables should be passed by reference in <b>include\DB_Functions.php</b> on line <b>1708</b><br />

我获取日期时间的声明是错误的?

1 个答案:

答案 0 :(得分:2)

问题是您正在使用bindParam()实际将变量绑定到查询中的参数。这必须是变量,而不是返回值的方法调用。

允许使用如下:

$value = 'somevalue';
$result->bindParam(':some_field', $value);
$value = 'someothervalue';
$result->execute(); // executes query with 'someothervalue' passed as parameter.

在您的情况下,您可能想要使用bindValue()。实际上,它以不可变的方式将值绑定到参数。或者将格式化的日期时间存储到不同的类变量中,并继续使用bindParam()与该新变量。