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 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂