Blackie
Blackie

Reputation: 53

Calculate actual total day from starting date to ending date

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?

Upvotes: 0

Views: 221

Answers (4)

Vinita Pawar
Vinita Pawar

Reputation: 89

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'];

Upvotes: 0

alberjoe
alberjoe

Reputation: 88

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

Upvotes: 0

Bhupendra
Bhupendra

Reputation: 258

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);

}                                                      

Upvotes: 0

Amal Murali
Amal Murali

Reputation: 76646

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

Upvotes: 4

Related Questions