How to Calculate the Difference Between Two Dates in PHP

In this short lesson we look at how to create a Bootstrap styled form allowing the user to enter two dates. We then calculate the time difference between the two dates.

Date Difference Example

STEP 1

We will design the HTML form first:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>Time Difference</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
   </head>
   <body>
      <div class="container">
         <form class="form-inline">
            <div class="form-group">
               <label for="date1">Date 1</label>
               <input type="date" class="form-control" id="date1" name="date1">
            </div>
            <div class="form-group">
               <label for="date2">Date 2</label>
               <input type="date" class="form-control" id="date2" name="date2">
            </div>
            <button type="submit" class="btn btn-default">Calculate</button>
         </form>
      </div>
   </body>
</html>

STEP 2

We now need to add an action and method to our form.

We could process the form on the same page or send the data to a processing script.

In this case I will send my data to a processing script called processDates.php

Line 10 has been updated to reflect this.

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>Time Difference</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
   </head>
   <body>
      <div class="container">
         <form class="form-inline" action="processDates.php" method="post">
            <div class="form-group">
               <label for="date1">Date 1</label>
               <input type="date" class="form-control" id="date1" name="date1">
            </div>
            <div class="form-group">
               <label for="date2">Date 2</label>
               <input type="date" class="form-control" id="date2" name="date2">
            </div>
            <button type="submit" class="btn btn-default">Calculate</button>
         </form>
      </div>
   </body>
</html>

STEP 3

We now need to create the processDates.php script.

<?php
if(!empty($_POST['date1']) && !empty($_POST['date2'])){
   $date1 = new DateTime($_POST['date1']);
   $date2 = new DateTime($_POST['date2']);
   $difference = $date1->diff($date2);
   echo "There are " . $difference->days  . " days between " . $date1->format('d-m-Y') . " and " . $date2->format('d-m-Y');
} else {
   echo "Two dates need to be supplied";
}

Line 2: we check if 2 dates have been entered. Note that we should be doing more sanitization checks for a live server. You will need to amend the script depending on your individual requirements.

Lines 3 and 4: we create new DateTime objects from the supplied dates.

Line 5: we can now calculate the difference between the two dates

Line 6: a message specifying the number of days between the two dates. Note that the entered dates have been given a format of d-m-Y representing day-month-year e.g. 27-04-2016. You can change this format to suit your own requirements and date format for your own country.

Line 7: an error message displayed if 2 dates are not supplied. This is part of the IF ELSE block (line 2).

Want to Learn More?

Become a member and get access to the member only area of this site here.

Comments

Your email address will not be published.

PHP for Beginners

Enrol in the full course here

PHP, MySQL and PDO

Coming soon, a full course on using PHP, MySQL and PDO. Be notified as soon as the course goes live.