dregvw1801 2013-03-21 06:03
浏览 75
已采纳

日期时间差异错误

I have a problem when I use difference of the datetime.

Here is the php code

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->days;

The correct result should be 2. But unfortunately it results 6015. Even when I change the date, its still 6015. Did you guys encounter this problem? I tried to run the script from other computer but its working.

  • 写回答

3条回答 默认 最新

  • duanchen7401 2013-03-21 06:21
    关注

    Try this,

    $start_date = new DateTime("2009-10-11");
    $end_date = new DateTime("2009-10-13");
    $interval = $start_date->diff($end_date);
    echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
    

    you use $interval->days replace with $interval->d." days "

    you can check my answer https://stackoverflow.com/a/14938421/718224 on date difference for more information.

    may this help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误