douxing1353 2015-07-20 14:51
浏览 136
已采纳

PHP MongoDate / DateTime错误或误解

Is there something wrong with the MongoDate feature?

When I convert a DateTime Object to MongoDate, and then I try to convert it back to DateTime, I get a totally different value. Keep in mind we're talking about a date within the EPOCH limits.

Here's a way to reproduce the issue.

$dateTime  = new DateTime( '2015-07-20 10:15:45', new DateTimeZone( 'Europe/London' ) );

$mongoDate = new MongoDate( $dateTime->getTimeStamp() );

echo $mongoDate->sec ."
"; // 1437383745

echo date( 'Y-m-d H:i:s', $mongoDate->sec ) ."
"; // 2015-07-20 11:15:45

$dateTime2 = $mongoDate->toDateTime();

echo $dateTime2->format('Y-m-d H:i:s') ."
"; // 1969-12-15 10:41:40

Is this behaviour normal?

  • 写回答

1条回答 默认 最新

  • douxuanjie2692 2015-07-20 16:30
    关注

    You have timezone issue. When you create DateTime object, you create it in Europe/London timezone. But when you "convert" seconds back to datetime format, you use date() function, which uses default timezone, which apparently is different from Europe/London.

    Create DateTime object with $mongoDate->sec and then convert timezone.

    $dt = new DateTime('@' . $mongoDate->sec);
    $dt->setTimezone(new DateTimeZone('Europe/London'));
    echo $dt->format('c');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退