Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何更改NOW()时区_Php_Mysql_Timezone_Prepared Statement - Fatal编程技术网

Php 如何更改NOW()时区

Php 如何更改NOW()时区,php,mysql,timezone,prepared-statement,Php,Mysql,Timezone,Prepared Statement,我正在sql查询中使用NOW()。我想把它改成另一个时区。可能吗 尝试了这个功能 class common { public function getTime() { $date = new DateTime(); $date->setTimezone(new DateTimeZone('Europe/Paris')); return $date->format('Y-m-d H:i:s'); } } 并得到以下错

我正在sql查询中使用NOW()。我想把它改成另一个时区。可能吗

尝试了这个功能

class common {

    public function getTime() {
        $date = new DateTime();
        $date->setTimezone(new DateTimeZone('Europe/Paris'));
        return $date->format('Y-m-d H:i:s');
    }

}
并得到以下错误

Catchable fatal error:  Object of class common could not be converted to string in line below

        $stmt = $this->db->prepare("INSERT INTO `items` 
      (`refno`, `color`, `size`, `qt`, `stackno`, `notes`, `price`, `add_date`)
      VALUES (?, ?, ?, ?, ?, ?, ?, $this->common->getTime())") or die($db->error);

我做错了什么?

您将使用此sql调用

SET time_zone = timezone;
根据mysql文档


该错误实际上是一个PHP错误,而不是SQL错误。尝试在字符串插值中计算复杂变量展开表达式的方式与您的方式不同

尝试将对象放在大括号中:

$stmt = $this->db->prepare("INSERT INTO `items` 
      (`refno`, `color`, `size`, `qt`, `stackno`, `notes`, `price`, `add_date`)
      VALUES (?, ?, ?, ?, ?, ?, ?, {$this->common->getTime()})") or die($db->error);
请参见手册页面中的副标题复杂(卷曲)语法


请回复您的评论:


现在在同一行上得到这个错误:尝试获取非对象的属性

您尚未显示如何设置
$this->common
。根据所显示的语法,common需要是类的对象实例
common
。我猜您正在尝试调用getTime()函数,而不实例化该类。如果要使用类的静态方法,则必须这样做:

class common {

    public static function getTime() {
        $date = new DateTime();
        $date->setTimezone(new DateTimeZone('Europe/Paris'));
        return $date->format('Y-m-d H:i:s');
    }

}

$stmt = $this->db->prepare("INSERT INTO `items` 
      (`refno`, `color`, `size`, `qt`, `stackno`, `notes`, `price`, `add_date`)
      VALUES (?, ?, ?, ?, ?, ?, ?, " . common::getTime() . ")") or die($db->error);

如果您不熟悉类和对象之间的差异,以及
static
的用法,那么您需要进行一些阅读,例如,..

MySQL的日期函数不直接执行时区,但您可以告诉服务器要使用哪个时区:现在在同一行上得到这个错误:尝试获取非的属性-object@epic,
common
可能未实例化。也许你想让
getTime()
是静态的,这样你就可以像
common::getTime()
那样调用它了?谢谢@MarcusAdams!现在我需要一个新的目标。:)