Calculate number of days remaining [duplicate]

心已入冬 提交于 2019-12-30 03:34:29

问题


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 :

7 days remaining... 6, 5, 4, etc

Can you help me please ?


回答1:


$future = strtotime('21 July 2012'); //Future date.
$timefromdb = //source time
$timeleft = $future-$timefromdb;
$daysleft = round((($timeleft/24)/60)/60); 
echo $daysleft;



回答2:


  $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



回答3:


SELECT DATEDIFF(yourtimestamp, CURDATE()) AS days

doc ref: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff




回答4:


$days = round((timestamp_from_database - time()) / 86400);



回答5:


$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");

http://php.net/manual/ro/function.date-diff.php



来源:https://stackoverflow.com/questions/7531686/calculate-number-of-days-remaining

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!