Hello!
If I have two variables $startDate="YYYYmmdd"
and $endDate="YYYYmmdd"
, how can I get the number of days between them please?
Thank you.
Hello!
If I have two variables $startDate="YYYYmmdd"
and $endDate="YYYYmmdd"
, how can I get the number of days between them please?
Thank you.
$DayDiff = strtotime("2010-01-12")-strtotime("2009-12-30");
echo date('z', $DayDiff)." Days";
this one should be precise and usable with PHP < 5.2
Here is the sample code
$startDate = mktime(0,0,0,1,1,2010);
$endDate = mktime(0,0,0,12,1,2010);
$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
echo "Differernce is $fullDays days";
If you are using PHP 5.3, you can use the new DateTime
class:
$startDate = new DateTime("20101013");
$endDate = new DateTime("20101225");
$interval = $startDate->diff($endDate);
echo $interval->days . " until Christmas"; // echos 73 days until Christmas
If not, you will need to use strtotime
:
$startDate = strtotime("20101013");
$endDate = strtotime("20101225");
$interval = $endDate - $startDate;
$days = floor($interval / (60 * 60 * 24));
echo $days . " until Christmas"; // echos 73 days until Christmas
Almost the same : http://stackoverflow.com/questions/2040560/how-to-find-number-of-days-between-two-dates-using-php. I think you can understand.
<?php
$time1=strtotime($startDate);
$time2=strtotime($endDate);
$daycount=floor(($time2-$time1)/ 86400);
?>
<?php
function days($date1, $date2) {
$date1 = strtotime($date1);
$date2 = strtotime($date2);
return ($date2 - $date1) / (24 * 60 * 60);
}
$date1 = '20100820';
$date2 = '20100930';
echo days($date1, $date2);
?>
The easiest way I have found to get the number of days between them is by converting the Start and End dates to Unix timestamps and doing an subtract on them.
Then if you want to format the date convert it back using the PHP date function.