代码之家  ›  专栏  ›  技术社区  ›  DDS

MongoDb等价于日期(从UNIXTIME(<<VALUE>>)和时间(从UNIXTIME(<<VALUE>>))

  •  0
  • DDS  · 技术社区  · 5 年前

    我有一个时间戳存储为unix时间(毫秒)和UTC查询参数的查询

    select *
    from my_table
    where parameter1 = 'Goofy'
    and DATE(FROM_UNIXTIME(time)) >= '2020-05-13 00:00:00.000 0000' --startdate
    and DATE(FROM_UNIXTIME(time)) <= '2020-05-15 00:00:00.000 0000' --enddate
    and TIME(FROM_UNIXTIME(time)) >= '01-01-1970 12:22:00.000 0000' --starttime
    and TIME(FROM_UNIXTIME(time)) <= '01-01-1970 19:33:00.000 0000' --endtime
    

    在Mongo(PHPAPI)中,我的查询如下

    $query = [
        '$and' => [
             ['parameter1' => 'Goofy']
             ,['time' => [$gte =>  <conversion to unixtime for date '2020-05-13 00:00:00.000 0000' >]]
             ,['time' => [$lte  => <conversion to unixtime for date '2020-05-15 00:00:00.000 0000' >]]
             ,['time' => [$gte  => <conversion to unixtime for time '01-01-1970 12:22:00.000 0000' >]]
             ,['time' => [$lte  => <conversion to unixtime for time '01-01-1970 19:33:00.000 0000' >]]
    ]
    ];
    
    $cursor = (new MongoDB\Client())->selectCollection('myDb', 'myTable')->find($query,[]);
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   Wernfried Domscheit    5 年前

    将日期和时间值分开看起来有点奇怪,通常您只使用同时包含日期和时间部分的日期时间值。

    无论如何,将Unix时间转换为 Date 价值很简单。请注意,Unix的时间是秒 1970-01-01 00:00:00Z 米莉 1970-01-01 00:00:00Z

    你的问题不是很清楚,可能是这个:

    'time' => [$gte => new Date("2020-05-15 12:22:00")]
    

    否则,将日期/时间值转换为整数应该在php本地完成。