問題描述
我不得不承認,我什至沒有嘗試自己編寫這個問題,這個問題對某些人來說可能很煩人,但我很驚訝我無法在網(wǎng)上找到我正在嘗試的好例子去做.也許我只是沒有在搜索中使用正確的關鍵詞.
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.
我正在嘗試計算從現(xiàn)在(頁面加載的時間)到特定日期和時間(例如,2011 年 5 月 11 日星期三下午 12:00)的剩余時間并顯示它.我原以為找到一個片段來實現(xiàn)這一點很容易,但到目前為止還沒有運氣.有沒有人有一些他們以前用來完成這個的示例代碼?我不希望有人從頭開始為我寫這篇文章,但如果我至少能指出正確的方向,那將非常有幫助.
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 函數(shù):
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 版本才能這樣做 - 否則,請執(zhí)行 helloandre 推薦.
You'll need a version of PHP that's at least 5.3 to do it this way - otherwise, do what helloandre recommends.
這篇關于php - 從當前頁面加載時間到特定時間的剩余時間的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!