php now 当地时间,php-如何更改NOW()时区

我在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);

我做错了什么?

解决方法:

该错误实际上是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);

发表您的评论:

now getting this error on the same line: Trying to get property of non-object

您尚未显示如何设置$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的使用,则需要阅读一些内容,例如Object-Oriented PHP.

标签:timezone,prepared-statement,mysql,php

来源: https://codeday.me/bug/20191201/2083299.html


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部