0

I have some simple question regarding about calculate the actual day from Starting Date to Ending Date.

Inside the calculation will include leap year, how many day in a month.

Example:
Starting Date : 15-03-2014
Ending Date   : 11-06-2015

Result: 453 days

I had read one question quite similar with this. But it is not I want.
How to calculate the difference between two dates using PHP?

Community
  • 1
  • 1
Blackie
  • 53
  • 1
  • 14
  • I prefer to use the Unix time stamp for all of these issues. The oddities of our calendar system is irrelevant when working with a more or less pure time symbol. Just use `$daysApart = ($date1-$date2)/86400` – rfoo Mar 15 '14 at 05:24
  • I'm working with some systems relevant with hotel. Therefore, I need to display the day left very accurate. – Blackie Mar 15 '14 at 06:38
  • possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Nanne Mar 15 '14 at 07:05
  • @Nanne, I had mentioned I read this question and the answer was not what I looking for. Please check the link I attached at my question. – Blackie Mar 16 '14 at 22:32

4 Answers4

4

The best way to achieve this would be using DateTime (and DateInterval) objects. Convert each date string into a DateTime object and use the DateTime::diff() method to calculate the number of days between the two:

$dt1 = new DateTime($date1);
$dt2 = new DateTime($date2);
$diff = $dt2->diff($dt1);
echo $diff->format("%a"); // => 453
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

using Jquery you can get actual days

var due = due_date_month + '/' + due_date_day + '/' + due_date_year;
var deliver = deliver_date_month + '/' + deliver_date_day + '/' + deliver_date_year;

var deliver_date = new Date(deliver);

var due_date = new Date(due);

if (due_date > deliver_date) {
var difference = Math.floor(due_date - deliver_date);

difference = parseInt(difference / 86400000);
alert(difference);

}                                                      
Bhupendra
  • 248
  • 2
  • 7
0

You could to convert the dates to unix time using strtotime(), to substract dates, and divide result for 86400 (seconds in a day), like this:

$start = strtotime($date1);
$end = strtotime($date2);
$diff = $end-$start;
$days = ceil($diff/86400); //or floor depends what you need

p.s.: $start and $end must be in US format

alberjoe
  • 88
  • 6
0
DATEDIFF('ending date','Starting date');

    Ex. 
$q=mysql_query('select DATEDIFF('$end_date','$start_date')' AS total_days <from table name>');
    $result=mysql_fetch_array($q);

    echo $result['total_days'];
Vinita Pawar
  • 89
  • 1
  • 11