我知道也有人问过类似的问题,但他们都没有适合我的解决方案.

我有一个名为Events的表,它有一个标题为"EVENT_FINISH_TIME"的列,一个时间戳.我需要获得在某个时间戳(通常是now()->format(...))之后结束的所有事件.我编写了以下代码作为测试:

$timestamp = "2020-11-22 13:00:00";
$events = Events::where('event_finish_time', '>=', '"'.$timestamp.'"');
dd($events->get("*")->toArray());

This code returns [].
Using toSql() and getBindings(), I get: select * from `events` where `event_finish_time` >= ? and "2020-11-22 13:00:00"
The combination (select * from `events` where `event_finish_time` >= "2020-11-22 13:00:00") runs perfectly fine from phpMyAdmin, and returns the correct result.
Setting the binding to 'TIMESTAMP("' . now() . '")' doesn't help with the php query and does not affect the direct SQL one.
What could be the cause of this issue? I have checked the spelling of all the tables and columns.

推荐答案

在Laravel中比较时间戳的正确语法比你现有的要简单.您不需要将时间戳用双引号括起来.

$timestamp = "2020-11-22 13:00:00";
$events = Events::where('event_finish_time', '>=', $timestamp)->get();
dd($events->toArray());

如果这不能解决问题,您可能还需要判断数据库中的时间戳与您正在比较的时间戳之间是否存在任何日期格式差异.

此外,确保Events表中的event_finish_time列属于TIMESTAMP类型或兼容的日期/时间类型.

Php相关问答推荐

如何在WooCommerce HPOS订单列表中创建自定义过滤器?

文件大小在php下载读取文件中不可见

PHP-转义字符串内的双反斜杠

Laravel;Composer安装突然返回选项快捷方式不能为空.

在Symfony 6.4中将工作流动态添加到服务

如何在Livewire 3 Form类中做依赖注入?

PHP:使用FORT从数据库获取USERS表到AUTH表

PHP:为什么递归需要这么长时间

Laravel Carbon DiffForHumans选项(年,月,天)

如何在laravel中添加延迟?

WooCommerce在收据页面获取产品下载URL

curl_close()未从PHP 8开始写入CURLOPT_COOKIEJAR会话文件

显示 woocommerce 中所选变体的属性术语描述

WooCommerce:如果购物车中已存在产品,则增加购物车商品数量

需要以一种形式执行两个操作

为什么 php 只能在我的 Nginx Web 服务器的某些目录中工作?

避免在 WooCommerce 中多次触发挂钩函数

Laravel 路由参数中的 ":" 是什么?

PHP文件如何从CSS执行

按前 3 列对二维数组中的行数据进行分组,然后用逗号连接每组中的其余列