How To Get The Difference Between Two Dates In PHP
Submitted by alpha_luna on Tuesday, April 12, 2016 - 10:27.
Good Day!!!
In this article, we are going to learn on How To Get The Difference Between Two Dates In PHP. In the PHP, to calculate the difference between two dates has many ways to do this. To get the difference of hours between two dates, we have to use the functions of datetime. We have the start date and end date to get their difference hour. strtotime() we are going to use to calculate the two dates difference.HTML Date Input
This is the input field of date to get the start date and end date came from the user.- <center>
- <form id="twoDatesDifference" action="" method="post">
- <div>
- <br />
- <input type="date" name="startOf_Date" value="<?php if(!empty($_POST["startOf_Date"])) { echo $_POST["startOf_Date"]; } ?>" class="date_textBox">
- </div>
- <div>
- <br />
- <input type="date" name="endOf_Date" value="<?php if(!empty($_POST["startOf_Date"])) { echo $_POST["endOf_Date"]; } ?>" class="date_textBox">
- </div>
- <div>
- <input type="submit" name="enter" value="Enter" class="btn_difference">
- </div>
- </form>
- </center>
PHP Function
This is the PHP function, and this is the codes that the strtotime() function is used to calculate the hours difference of two dates given by the user.- <?php
- function differenceIn_TwoDates($startOf_Date,$endOf_Date){
- return $difference_Dates;
- }
- $dates_difference = differenceIn_TwoDates($_POST["startOf_Date"],$_POST["endOf_Date"]);
- echo "<h3 style='color:red; text-align:center;'>The Difference of Hour in Two Dates is <b style='color:blue;'>" . $dates_difference . " hours.</b></h3>";
- }
- ?>
- <style type="text/css">
- #twoDatesDifference {
- border-top:red 2px solid;
- padding:10px;
- width:500px;
- }
- #twoDatesDifference div {
- margin-bottom: 15px
- }
- #twoDatesDifference div label {
- margin-left: 5px
- }
- .date_textBox {
- padding:10px;
- border:#F0F0F0 1px solid;
- border-radius:4px;
- background-color:#FFF;
- cursor:pointer;
- font-size:18px;
- }
- .btn_difference {
- background-color:azure;
- color:black;
- font-weight:bold;
- font-size:18px;
- border:red 1px solid;
- border-radius:4px;
- padding:10px;
- width:200px;
- cursor:pointer;
- }
- .btn_difference:hover {
- background-color:black;
- color:azure;
- font-weight:bold;
- font-size:18px;
- border:red 1px solid;
- border-radius:4px;
- padding:10px;
- width:200px;
- cursor:pointer;
- }
- </style>
Add new comment
- 177 views