php - 从当前页面加载时间到特定时间的剩余时间
我不得不承认,我什至没有尝试自己编写这个问题,这个问题对某些人来说可能很烦人,但我很惊讶我无法在网上找到我正在尝试的好例子去做.也许我只是没有在搜索中使用正确的关键词.
I have to admit that having not even tried to code this myself this question may be a annoying to some but I'm very surprised that I wasn't able to find a good example on the web of what I'm trying to do. Perhaps I'm just not using the right key words in my searches.
我正在尝试计算从现在(页面加载的时间)到特定日期和时间(例如,2011 年 5 月 11 日星期三下午 12:00)的剩余时间并显示它.我原以为找到一个片段来实现这一点很容易,但到目前为止还没有运气.有没有人有一些他们以前用来完成这个的示例代码?我不希望有人从头开始为我写这篇文章,但如果我至少能指出正确的方向,那将非常有帮助.
I am trying to calculate the time remaining from now (the time the page loads) until a specific date and time (let's say Wednesday May 11, 2011 12:00PM for example) and display it. I had thought it would be quite easy to find a snippet to accomplish this but no luck so far. Does anyone have some example code they have used to accomplish this before? I don't expect anyone to write this for me from scratch but if I can at least get pointed in the right direction it would be extremely helpful.
推荐答案
我会使用 DateInterval 和 DateTime 函数:
I'd use the DateInterval and DateTime functions:
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%a days, %h hours, %i minutes, %s seconds");
您需要至少 5.3 的 PHP 版本才能这样做 - 否则,请执行 helloandre 推荐.
You'll need a version of PHP that's at least 5.3 to do it this way - otherwise, do what helloandre recommends.
相关文章