【问题标题】:mongodb date not matching with the given valuemongodb 日期与给定值不匹配
【发布时间】:2015-06-20 11:16:55
【问题描述】:

在将数据插入 mongodb 集合时,我输入日期为“20-06-2015” 然后使用以下命令将其转换为 mongo 格式:

new MongoDate(strtotime(ClearContent(date("Y-m-d",strtotime($start_date)))));

但现在当我签入数据库时​​,它会显示:

ISODate("2015-04-19T18:30:00.000Z")

为什么是昨天的日期。

【问题讨论】:

    标签: php mongodb date mongodate


    【解决方案1】:

    mongodb 中的日期以 UTC 格式存储,您可能在 PHP 中使用了不同的时区

    尝试在 PHP 脚本的开头设置此项以使用 UTC 时区:

    date_default_timezone_set('UTC');
    

    【讨论】:

      【解决方案2】:

      试试这个...

      $dat = new DateTime(date("Y-m-d",strtotime($start_date)), new DateTimeZone('UTC'));
      $get = $dat->getTimestamp();
      $date= new MongoDate($get);
      

      【讨论】:

        【解决方案3】:

        这会起作用。

        $d = new \MongoDate();
        $mongodate = date('Y-m-d H:i:s', $d->sec);
        date_default_timezone_set("Asia/Kolkata");
        $date = date('Y-m-d H:i:s');
        $time = strtotime($date) - strtotime($mongodate);
        $mongodate_time = new \MongoDate($d->sec + $time, $d->usec);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-01-24
          • 1970-01-01
          • 1970-01-01
          • 2021-12-22
          • 2020-12-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多