2016-11-11 56 views
1

转换ISO日期蒙戈我有我的蒙戈文档中的下列数据:问题使用PHP

"last_assigned" : ISODate("2016-11-10T20:34:36.000Z") 

当我查询,其中包括日期,如上述文件的数据库,我试图将其转换后,这是我的var_dump显示:

object(DateTime)[22] 
    public 'date' => string '1970-01-01 00:00:00.000000' (length=26) 
    public 'timezone_type' => int 1 
    public 'timezone' => string '+00:00' (length=6) 

这是我的代码看起来像 - “[ 'last_assigned'] $值” 是我从DB回来...

  if (!empty($value['last_assigned'])) { 
       $tempdate = new MongoDate(strtoTime($value['last_assigned'])); 
       var_dump($tempdate->toDateTime());     
      } 

我如何编写我的代码,以便它能够显示日期的适当值?

编辑1

我试图改变这样的代码:

   $tempdate = (string)$value['last_assigned']->sec; 
       $tempdate = new MongoDate(strtotime($tempdate)); 
       var_dump($tempdate->toDateTime()); 

这就是vardump显示:

object(DateTime)[22] 
    public 'date' => string '1970-01-01 00:00:00.000000' (length=26) 
    public 'timezone_type' => int 1 
    public 'timezone' => string '+00:00' (length=6) 
+0

尝试将last_assigned转换为字符串,然后再通过strtoTime函数对其进行设置。 – Veeram

+0

你使用的是mongo php驱动程序吗?或者你只是直接查询数据?如果直接查询数据,则可能需要将日期保存为字符串“2016-11-10T20:34:36.000Z”。所以实质上,新的MongoDate(strtoTime(“2016-11-10T20:34:36.000Z”)应该是这样的。 – Veeram

+0

@Veeram我使用的是mongoclient驱动程序,我知道它已被弃用...我们将尽快升级一切,但现在......这就是我正在使用的 – Happydevdays

回答