Getting The Difference of Two Dates Using PHP

This tutorial will teach you on how to get the difference of the two dates using PHP datetime function. You will also learn in this tutorial on how to use tigra calendar in making popup date. To understand more fallow the step bellow.

Creating Our Form and Connecting Tigra Javascript

The code bellow will give us the form that allow us to pick two date to be subtracted. The selection of date is powered by tigra popup calendar. Copy the code bellow and save it as "index.php".
  1. <link rel="stylesheet" type="text/css" href="tcal.css" />
  2. <script type="text/javascript" src="tcal.js"></script>
  3. <form action="calculate.php" method="post">
  4. Date 1 : <input type="text" name="date1" class="tcal" /><br>
  5. Date 2 : <input type="text" name="date2" class="tcal" /><br><input type="submit" value="calculate" />
  6. </form>

Writing Our Script that Calculate The Two Dates

This code will Calculate the two dates using the datetime function in PHP. Copy the code bellow and save it as "calculate.php".
  1. <?php
  2. $date1=$_POST['date1'];
  3. $date2=$_POST['date2'];
  4. $datetime1 = new DateTime($date1);
  5.  
  6. $datetime2 = new DateTime($date2);
  7.  
  8. $difference = $datetime1->diff($datetime2);
  9.  
  10. echo $difference->days;
  11. ?>
That's it you've been successfully created a system that gets t6he difference of two dates. Thye tigra javascript and css are included in the zip file attached with this tutorial.

Comments

Fatal error: Call to undefined method DateTime::diff() in C:\xampp\htdocs\Program\date\calculate.php on line 6 that's the error when running the program sir can you fixed what is the problem. i need that thank you. my email [email protected]

Add new comment