-1

I have two dates like this, how to calculate left days in between dates using php.

$date1='2016-12-26';
$date2='2017-03-21';
user3544256
  • 201
  • 1
  • 3
  • 9
  • 1
    There are dozens of similar questions in SO. Initially, try yourselves from php.net manual, then check this SO forum when you face issues. Finally raise question when you are unable to. – Thamilhan Mar 09 '17 at 07:29
  • 1
    `http://php.net/manual/en/function.date-diff.php` this will help – devpro Mar 09 '17 at 07:29
  • Can you show us what you have tried? – Jerodev Mar 09 '17 at 07:29
  • 5
    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) – devpro Mar 09 '17 at 07:30

2 Answers2

2

Refer to date_diff .Try this:

$d1 = new DateTime($date1);
$d2 = new DateTime($date2);

$diff = $d2->diff($d1);
echo $diff->days;  // 85
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
0

You can use this if date_diff doesn't work.Use this function if you want to get the dates between the date1 and date2 or if u want the left days between the two given dates then you can increment a number like $i below and u ca return the $i value.

    function date_range($date1, $date2, $step = '+1 day', $output_format = 'Y-m-d') {
      $dates = array();
      $current = strtotime($first);
      $last = strtotime($last);
      $i=1;
      while( $current <= $last ) {
        $dates[] = date($output_format, $current);
        $dateq = date($output_format, $current);
        $current = strtotime($step, $current);
        $i++;
      }        
      //return $dates; if u want the dates 
      return $i; //if u want the count of dates between the tow given dates
    }

Hope this help you..

shaan
  • 96
  • 9