Php 如何比较日期和时间

Php 如何比较日期和时间,php,cakephp,mariadb,Php,Cakephp,Mariadb,我在处创建了一个名为的字段,其类型为时间戳(预定义为当前时间戳),当我运行下面的代码时,查询不起作用。该代码应在1分钟内将所有日志带给我 $sent=$this->Logs->find()->where(['Logs.created_at>='=>date('Y-m-d H:i:s',strottime('-1分钟'))))->where(['Logs.user_id='=>this->Auth->user('id'),'Logs.status='=>1])->all() 但是,如果我将字段类型

我在处创建了一个名为
的字段,其类型为
时间戳
(预定义为
当前时间戳
),当我运行下面的代码时,查询不起作用。该代码应在1分钟内将所有日志带给我

$sent=$this->Logs->find()->where(['Logs.created_at>='=>date('Y-m-d H:i:s',strottime('-1分钟'))))->where(['Logs.user_id='=>this->Auth->user('id'),'Logs.status='=>1])->all()

但是,如果我将字段类型更改为
datetime
,它将正常工作。关键是我需要字段类型为
timestamp
,而不是datetime

SQL调试 表格记录

更新 我意识到DB中的记录是
2021-05-02 14:58:26
,但当我获取PHP时,它显示为
2021-05-02 17:58:26

请参见上图和下面的输出

object(Cake\ORM\ResultSet)#210 (1) { ["items"]=> array(1) { [0]=> object(App\Model\Entity\Log)#203 (19) { ["id"]=> int(4315) ["notify_to"]=> string(12) "558788085188" ["notify_from"]=> string(4) "3513" ["content"]=> string(4) "fdfd" ["attachment"]=> NULL ["status"]=> int(1) ["error"]=> NULL ["user_id"]=> int(1) ["typeof"]=> NULL ["created_at"]=> object(Cake\I18n\FrozenTime)#220 (3) { ["time"]=> string(32) "2021-05-02 17:58:26.000000+00:00" ["timezone"]=> string(3) "UTC" ["fixedNowTime"]=> bool(false) } ["[new]"]=> bool(false) ["[accessible]"]=> array(9) { ["notify_to"]=> bool(true) ["notify_from"]=> bool(true) ["content"]=> bool(true) ["attachment"]=> bool(true) ["user_id"]=> bool(true) ["typeof"]=> bool(true) ["created_at"]=> bool(true) ["status"]=> bool(true) ["error"]=> bool(true) } ["[dirty]"]=> array(0) { } ["[original]"]=> array(0) { } ["[virtual]"]=> array(0) { } ["[hasErrors]"]=> bool(false) ["[errors]"]=> array(0) { } ["[invalid]"]=> array(0) { } ["[repository]"]=> string(4) "Logs" } } } 
PHP测试
当我调用PHP函数时,
date()
返回正确的时间。

对于使用CakePHP3.x的用户,您需要设置
date\u default\u time\u zone()
,但奇怪的是,您还需要为数据库实例设置
timezone

app_local.php

'Datasources' => [
    'default' => [
        'host' => 'localhost',
        /*
         * CakePHP will use the default DB port based on the driver selected
         * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
         * the following line and set the port accordingly
         */
        //'port' => 'non_standard_port_number',
        'username' => 'xxxx',
        'password' => 'xxxxx',
        'database' => 'xxxxx',
        'log' => true,
        'url' => env('DATABASE_URL', null),
        'timezone' => '-3:00',
    ],
]

当CakePHP从数据库中检索记录时,它会根据
数据源上设置的时区进行更改

此源代码生成并执行的实际SQL查询是什么?您可以为任何记录添加数据库截图吗?您能给我们显示一个包含记录的数据库图像吗?@Rohittal更新了question@RohitMittal我已经用更重要的解释更新了问题。这正是应该如何工作的,不是CakePHP做的。MySQL/MariaDB将在保存时将值转换为UTC,在读取时将值转换为当前时区。这正是为什么人们首先会在
DATETIME
上使用
TIMESTAMP
。如果不需要该功能,则不需要
时间戳
类型。
'Datasources' => [
    'default' => [
        'host' => 'localhost',
        /*
         * CakePHP will use the default DB port based on the driver selected
         * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
         * the following line and set the port accordingly
         */
        //'port' => 'non_standard_port_number',
        'username' => 'xxxx',
        'password' => 'xxxxx',
        'database' => 'xxxxx',
        'log' => true,
        'url' => env('DATABASE_URL', null),
        'timezone' => '-3:00',
    ],
]