Php 获取距离日期还剩多少天和小时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7474762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Php get how many days and hours left from a date
提问by John123
I have a created_at date saved liked this "2011-09-23 19:10:18" And I want to get the days and hours left until the date is reached. How do I do that? and column name in database remain days automatically update daily with remain days, please solve this
我保存了一个 created_at 日期,就像这个“2011-09-23 19:10:18”我想得到到达日期之前的剩余天数和小时数。我怎么做?和数据库中的列名剩余天数每天自动更新剩余天数,请解决这个问题
回答by Rudu
PHP fragment:
PHP片段:
<?php
//Convert to date
$datestr="2011-09-23 19:10:18";//Your date
$date=strtotime($datestr);//Converted to a PHP date (a second count)
//Calculate difference
$diff=$date-time();//time returns current time in seconds
$days=floor($diff/(60*60*24));//seconds/minute*minutes/hour*hours/day)
$hours=round(($diff-$days*60*60*24)/(60*60));
//Report
echo "$days days $hours hours remain<br />";
?>
Note the hour-round and no minutes/seconds consideration means it can be slightly inaccurate.
请注意以小时为单位,不考虑分钟/秒意味着它可能会稍微不准确。
回答by Korvin Szanto
This should seed your endeavor.
这应该是你努力的种子。
getdate(strtotime("2011-09-23 19:10:18"))
Full conversion:
完全转换:
$seconds = strtotime("2011-09-23 19:10:18") - time();
$days = floor($seconds / 86400);
$seconds %= 86400;
$hours = floor($seconds / 3600);
$seconds %= 3600;
$minutes = floor($seconds / 60);
$seconds %= 60;
echo "$days days and $hours hours and $minutes minutes and $seconds seconds";
回答by CountZero
as of PHP 5.3.0 you could use build-in Date object:
从 PHP 5.3.0 开始,您可以使用内置 Date 对象:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
回答by Vladimir Veljkovic
The easiest way is improved answer from CountZero. I used this solution for counter for time remaining before expiration of offer. Addition to first three lines of CountZero code:
最简单的方法是从 CountZero 改进答案。我使用这个解决方案来计算报价到期前的剩余时间。除了前三行 CountZero 代码:
$days = $interval->d;
$hours = $interval->h;
$minutes = $interval->i;
$seconds = $interval->s;
And now, you can use string functions to moderate your return values, to merge all or add '0' or '00' in front of the values.
现在,您可以使用字符串函数来调节返回值、合并所有值或在值前添加“0”或“00”。
回答by ahoura
it would be something like
它会是这样的
echo $date = date("Y-m-d H:i:s");echo "\n";
$original=time($date);
$modified = "2011-09-23 19:10:18";
echo date("Y-m-d H:i:s",$modified);echo "\n";
回答by Viral
You can find current date and time using date() function and then subtract
您可以使用 date() 函数查找当前日期和时间,然后减去
$x = 2011-09-23 - current_date
$x = 2011-09-23 - current_date
this will give you no. of days left.
这会给你没有。剩下的天数。
Do same with time as well ..
时间也一样..
Hope this helps
希望这可以帮助