当我显示 BETWEEN 子句时,DateTime() 方法不起作用 - Mysql/PHP
我有一个 PHP 函数,它执行一个定义了一些变量的 MySQL 查询。我需要 SQL 语句的 BETWEEN 子句来处理变量,其中变量之一是 DateTime() 函数。
这里的$start
变量应该创建一个新的时间,然后我在数据库中的值应该在这个时间和我定义的$finish
变量之间。
$start = new DateTime();
$finish = '2013-10-06 17:06:52';
$value = $this->GetOffset();
$this->db->select("esolar + $value AS Esolar", 1)
->from('calcdata')
->where('siteid', $siteid)
->where("time BETWEEN '$start' AND '$finish'")
->where('esolar <', 1000000)
->where('esolar <>', 0);
$query1 = $this->db->get()->result_array();
$Esolar1 = $query1[0]['Esolar'];
但是,当我这样做时,我收到此错误:
遇到 PHP 错误
严重性:4096 消息:DateTime 类的对象无法转换为字符串
谢谢
I have a PHP function that executes a MySQL query with a few variables defined. I need the BETWEEN
clause of the SQL statement to work with the variables, where one of the variables is the DateTime()
function.
Here the $start
variable should be making a new time and then the value I have in my database should be between this time and the $finish
variable i have defined.
$start = new DateTime();
$finish = '2013-10-06 17:06:52';
$value = $this->GetOffset();
$this->db->select("esolar + $value AS Esolar", 1)
->from('calcdata')
->where('siteid', $siteid)
->where("time BETWEEN '$start' AND '$finish'")
->where('esolar <', 1000000)
->where('esolar <>', 0);
$query1 = $this->db->get()->result_array();
$Esolar1 = $query1[0]['Esolar'];
However when I do it I get this error:
A PHP Error was encountered
Severity: 4096
Message: Object of class DateTime could not be converted to string
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你应该这样做:
否则你将传递一个对象而不是字符串作为参数
You should do something like:
otherwise you will pass an object instead of a string as a parameter
您的
$start
属于 DateTime 类 - 您需要将其转换为字符串...这可以使用
format
方法来完成 ->$start->format('Ymd H:i:s');
your
$start
is of DateTime class - you need to convert it to string ...This can be done using the
format
method ->$start->format('Y-m-d H:i:s');
这是因为要将 DateTime 对象转换为字符串形式,您必须使用它的 format()< /a> 方法。在你的情况下,它会变成这样:
This is because to convert the DateTime object to the string form you have to use it's format() method. In your case it will become something like this:
我看到
$start
未初始化为任何值,因此它包含当前日期/时间。鉴于此,您可以更改查询,而
无需使用
DateTime
对象I see
$start
is not initialized to any value, so it contains current date/time.Given this you could change query with
without having to use
DateTime
object