php 计算剩余天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7531686/
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
Calculate number of days remaining
提问by BobbaFett
I would like to calculate the number of days remaining before a date. In my database I have a timestamp corresponding to the end date. For example Friday 30. I would like to say something like that :
我想计算一个日期前剩余的天数。在我的数据库中,我有一个对应于结束日期的时间戳。例如星期五 30。我想说这样的话:
7 days remaining... 6, 5, 4, etc
剩余 7 天... 6, 5, 4, 等等
Can you help me please ?
你能帮我吗 ?
回答by Mob
$future = strtotime('21 July 2012'); //Future date.
$timefromdb = //source time
$timeleft = $future-$timefromdb;
$daysleft = round((($timeleft/24)/60)/60);
echo $daysleft;
回答by Muthu Kumar
$date1 = new DateTime("2016-01-01"); //current date or any date
$date2 = new DateTime("2016-12-31"); //Future date
$diff = $date2->diff($date1)->format("%a"); //find difference
$days = intval($diff); //rounding days
echo $days;
//it return 365 days omitting current day
回答by Mircea Soaica
$days = round((timestamp_from_database - time()) / 86400);
回答by Marc B
SELECT DATEDIFF(yourtimestamp, CURDATE()) AS days
doc ref: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
文档参考:http: //dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
回答by Vitalicus
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");