PHP - Get The Different Between Two Dates

Submitted by: 
Language: 
Visitors have accessed this post 324 times.

In this tutorial we will create a Get The Different Between Two Dates using PHP. PHP is a server-side scripting language designed primarily for web development. It is a lean and consistent way to access databases. This means developers can write portable code much easier. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Get The Different Between Two Dates</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  19. <form action="" method="POST">
  20. <div class="form-group">
  21. <label>Start Of Date</label>
  22. <input type="date" name="start" class="form-control"/>
  23. </div>
  24. <div class="form-group">
  25. <label>End Of Date</label>
  26. <input type="date" name="end" class="form-control"/>
  27. </div>
  28. <button class="btn btn-primary btn-block" name="enter">Enter</button>
  29. </form>
  30. <?php include 'calculate_dates.php'?>
  31. </div>
  32. </div>
  33. </body>
  34. </html>

Creating the Main Function

This is the main function of the application. This code will calculate the different between the two dates then return it as hour format. To do that copy and write inside the text editor, then save it as calculate_dates.php.

  1. <?php
  2. function differenceIn_TwoDates($startOf_Date,$endOf_Date){
  3. $timestamp_start = strtotime($startOf_Date);
  4. $timestamp_end = strtotime($endOf_Date);
  5. $difference_Dates = abs($timestamp_end - $timestamp_start)/3600;
  6. return $difference_Dates;
  7. }
  8. if(ISSET($_POST["enter"])) {
  9. $start = strtotime($_POST['start']);
  10. $end = strtotime($_POST['end']);
  11. $calculation = abs($end - $start)/3600;
  12.  
  13. echo "<center><h3>There are <i style='color:red;'>".$calculation."</i> Hours Different Between Two Dates</h3></center>";
  14. }
  15. ?>

There you have it we successfully created Get The Different Between Two Dates using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.