doumu6941 2014-03-16 23:42
浏览 12
已采纳

计算未显示正确天数的天数

I need help.. Is this right?

Start Date: Mar 16, 2014

End Date: Mar 19, 2014

Results: 2 Days

$plantEnd = get_the_author_meta('plantEnd', $sellerID );
$plantStart = get_the_author_meta('plantStart', $sellerID );

$future = $plantEnd;

$d = new DateTime($future);
echo $d->diff(new DateTime())->format('%a').' Days';

Why does it says 2 days? Isn't it 3 days? Im confused..

  • 写回答

2条回答 默认 最新

  • doutan3371 2014-03-17 00:04
    关注

    Since you aren't actually using $plantStart in your code and instead using the current time, you're basically getting a difference between now (the time the script was run, on server's time zone) and the start of Mar 19, 2014 (0h:0m:0s). So what you are really getting is something like 2 days 5 hours 3 minutes 25 seconds (depending on when you run it vs. server time.

    for example, when I run this locally:

    $d->diff(new DateTime())->format('%d:%H:%i:%s');

    I get 2:04:59:25

    So there's more to it than just getting that "2" returned.. you're just not formatting for it.

    And again, you aren't actually using the $plantStart anywhere either. So if you were to do this:

    <?php
    $plantEnd = '2014-03-19';//get_the_author_meta('plantEnd', $sellerID );
    $plantStart = '2014-03-16'; //get_the_author_meta('plantStart', $sellerID );
    
    $future = $plantEnd;
    
    $d = new DateTime($future);
    echo $d->diff(new DateTime($plantStart))->format('%d:%H:%i:%s');
    ?>
    

    You will see it outputs 3:00:0:0 (or you could continue to just use %d and get the "3"). This is because $plantStart (presumably - based on your post) just specifies yyyy-mm-dd, so passing just the yyyy-mm-dd value will put the hh:mm:ss at 0:0:0 (beginning of day) , so it will be a full day's calculation, which has the effect of "rounding up" to the whole day increment.

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

报告相同问题?

悬赏问题

  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了