Speed up development with full-stack environments for every branch.

Learn More

How to calculate the difference between two dates [PHP]

708 Runs 4366 Views 1842 Copies
Saved

Saved

dipesh.parmar 55

dipesh.parmar
published 4 years ago

<div style="margin-left:50px;">
  <h2>Calculate the difference between two dates</h2>
  <p>
    we will use DateTime and DateInterval objects for this.
  </p>
  <?
    /*
      create two date object named
      $date1
      And 
      $date2
    */
    $date1 = new DateTime("2013-10-25");
    $date2 = new DateTime("2013-03-31");
    
    /*
      below code returns the difference between two DateTime objects
    */
    $interval = $date1->diff($date2);

    echo "<strong>Difference :</strong> " 
          . $interval->y . " years, "
          . $interval->m ." months, "
          . $interval->d  ." days "; 
  ?>
</div>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).