4

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 ?

BobbaFett
  • 73
  • 1
  • 1
  • 4

5 Answers5

19
$future = strtotime('21 July 2012'); //Future date.
$timefromdb = //source time
$timeleft = $future-$timefromdb;
$daysleft = round((($timeleft/24)/60)/60); 
echo $daysleft;
Mob
  • 10,958
  • 6
  • 41
  • 58
10
  $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
Muthu Kumar
  • 107
  • 1
  • 2
6
$days = round((timestamp_from_database - time()) / 86400);
Stanislav Shabalin
  • 19,028
  • 3
  • 18
  • 18
Mircea Soaica
  • 2,829
  • 1
  • 14
  • 25
5
SELECT DATEDIFF(yourtimestamp, CURDATE()) AS days

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

Marc B
  • 356,200
  • 43
  • 426
  • 500
-1
$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

Vitalicus
  • 1,188
  • 13
  • 15