php-从页面加载的当前时间到指定时间为止的剩余时间 [英] php - Time remaining until specific time from current time of page load
问题描述
我不得不承认,甚至没有尝试自己编写这个问题,这对于某些人来说可能是一个烦人的问题,但令我感到惊讶的是,我无法在网络上找到我正在尝试的方法的好例子去做.也许我只是在搜索中没有使用正确的关键词.
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:00PM)的剩余时间并显示它.我以为找到一个片段来完成此任务很容易,但到目前为止还算不上运气.有没有人有一些示例代码,他们以前曾经用来完成此任务?我不希望有人从头为我写这篇文章,但是如果我至少能指出正确的方向,那将非常有帮助.
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.
推荐答案
我会使用 DateTime 函数:
$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版本才能执行此操作-否则,请执行
You'll need a version of PHP that's at least 5.3 to do it this way - otherwise, do what helloandre recommends.
这篇关于php-从页面加载的当前时间到指定时间为止的剩余时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!