倒数天没有显示正确的天数 [英] Counting down days not showing the right number of days
问题描述
我需要帮助..这是对的吗?
I need help.. Is this right?
开始日期:2014年3月16日
Start Date: Mar 16, 2014
结束日期:2014年3月19日
End Date: Mar 19, 2014
结果:2天
$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';
为什么说2天?不是3天吗?我感到困惑..
Why does it says 2 days? Isn't it 3 days? Im confused..
推荐答案
由于你实际上并没有使用 $ plantStart
得到的就是2天5小时3分25秒(取决于运行时间与服务器时间的相关程度。
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');
我得到 2:04:59:25
所以还有更多的只是得到2返回..你只是不格式化。
So there's more to it than just getting that "2" returned.. you're just not formatting for it.
而且,你还没有在任何地方使用 $ plantStart
,所以如果你这样做:
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');
?>
你会看到它输出 3:00:0:0
(或者你可以继续使用%d
并获得 3)。这是因为 $ plantStart
(大概是根据你的帖子)只是指定 yyyy-mm-dd
,所以传递 yyyy-mm-dd
值会将 hh:mm:ss
0: 0:0
(开始的一天),所以这将是一整天的计算,这具有舍入到全天的增量的效果。
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.
这篇关于倒数天没有显示正确的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!