Finding the number of days between two dates

2018-12-31 03:09发布

How to find number of days between two dates using PHP?

标签: php
26条回答
梦醉为红颜
2楼-- · 2018-12-31 04:00

Easy to using date_diff

$from=date_create(date('Y-m-d'));
$to=date_create("2013-03-15");
$diff=date_diff($to,$from);
print_r($diff);
echo $diff->format('%R%a days');
查看更多
后来的你喜欢了谁
3楼-- · 2018-12-31 04:01
$datediff = floor(strtotime($date1)/(60*60*24)) - floor(strtotime($date2)/(60*60*24));

and, if needed:

$datediff=abs($datediff);
查看更多
忆尘夕之涩
4楼-- · 2018-12-31 04:01

number of days between two dates in PHP

      function dateDiff($date1, $date2)  //days find function
        { 
            $diff = strtotime($date2) - strtotime($date1); 
            return abs(round($diff / 86400)); 
        } 
       //start day
       $date1 = "11-10-2018";        
       // end day
       $date2 = "31-10-2018";    
       // call the days find fun store to variable 
       $dateDiff = dateDiff($date1, $date2); 

       echo "Difference between two dates: ". $dateDiff . " Days "; 
查看更多
旧人旧事旧时光
5楼-- · 2018-12-31 04:01

If you want to echo all days between the start and end date, I came up with this :

$startdatum = $_POST['start']; // starting date
$einddatum = $_POST['eind']; // end date

$now = strtotime($startdatum);
$your_date = strtotime($einddatum);
$datediff = $your_date - $now;
$number = floor($datediff/(60*60*24));

for($i=0;$i <= $number; $i++)
{
    echo date('d-m-Y' ,strtotime("+".$i." day"))."<br>";
}
查看更多
呛了眼睛熬了心
6楼-- · 2018-12-31 04:03

Convert your dates to unix timestamps, then substract one from the another. That will give you the difference in seconds, which you divide by 86400 (amount of seconds in a day) to give you an approximate amount of days in that range.

If your dates are in format 25.1.2010, 01/25/2010 or 2010-01-25, you can use the strtotime function:

$start = strtotime('2010-01-25');
$end = strtotime('2010-02-20');

$days_between = ceil(abs($end - $start) / 86400);

Using ceil rounds the amount of days up to the next full day. Use floor instead if you want to get the amount of full days between those two dates.

If your dates are already in unix timestamp format, you can skip the converting and just do the $days_between part. For more exotic date formats, you might have to do some custom parsing to get it right.

查看更多
柔情千种
7楼-- · 2018-12-31 04:03
    // Change this to the day in the future
$day = 15;

// Change this to the month in the future
$month = 11;

// Change this to the year in the future
$year = 2012;

// $days is the number of days between now and the date in the future
$days = (int)((mktime (0,0,0,$month,$day,$year) - time(void))/86400);

echo "There are $days days until $day/$month/$year";
查看更多
登录 后发表回答