这是这个问题的后续问题:Counting down days not showing the right number of days
我仍然对日期和时间感到困惑。
设置开始和结束时间:
// start date: set the time of when you click the link
$startTime = strtotime('now');
$plantStart = date('M d, Y h:i:s', $startTime);
// end date: 3 days from the time of when you click the link
$date = strtotime("+3 day", $startTime);
$plantEnd = date('M d, Y h:i:s', $date);这给了我:
Mar - 17,2014 07:33:45 (开始)
Mar - 20,2014 07:33:45 (完)
现在,问题是..当我这样做的时候:
// show how many days/hours till $plantEnd date
$d = new DateTime($plantEnd);
$daysHours = $d->diff(new DateTime())->format('%d Days, %H Hours');
echo $daysHours;结果总是这样的:2天11小时它从不3天0小时或2天23小时..它是否仍然只获取到第三天0:00:00的时间,而不是精确到时间的一分钟?
发布于 2014-03-18 03:50:02
正如Yohann Tilotti在评论中提到的,问题是new DateTime()永远不会在diff()函数中初始化。
您的意思可能是:$d->diff(new DateTime($plantStart))。
您可以在此处看到一个正在运行的示例:http://ideone.com/FU8akb
发布于 2014-03-18 03:54:48
就像我评论的那样,坚持使用一种日期方法。首选的方法是DateTime
$plantStart = new DateTime('now');
echo $plantStart->format('Y-m-d H:i:s');
// Output: 2014-03-17 12:56:00
$plantEnd = new DateTime('now + 3 days');
echo $plantEnd->format('Y-m-d H:i:s');
// Output: 2014-03-20 12:56:00
$daysHours = $plantEnd->diff(new DateTime())->format('%d Days, %H Hours');
echo $daysHours;
// Output: 3 Days, 00 Hourshttps://stackoverflow.com/questions/22463864
复制相似问题