In this article, we are going to create How To Get Hours in Two Different Dates using PHP. With the help of this code, you can get the hours between two different dates. and in this post we are using two PHP Function to get the exact value first function is strtotime() and second is abs(). strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp and abs() fuction is also built-in function in PHP and is used to return the absolute (positive) value of a number. The abs() function in PHP is identical to what we call modulus in mathematics. The modulus or absolute value of a negative number is positive.
Getting Bootstrap
Bootstrap can be Download to your needs from their Getting Started page but I would prefer using the CDN option, because it is faster also it is advised to go through and get yourself accustomed with some bootstrap terms, including common classes. This page also some Examples of how to use Bootstrap classes.
Creating Layout To get Result
in this step are going to create form using HTML,CSS and bootstrap to get the values. here the source code you can save it as index.php to your project folder.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/> <title>How To Get Hours in Two Differents Dates using PHP</title> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/> <style type="text/css"> body{ margin-top:50px; background-color:rgba(245,245,245,1); } hr{ border-top:1px dotted #ccc; } .well{ -webkit-box-shadow: 2px 4px 12px -2px rgba(0,0,0,0.75); -moz-box-shadow: 2px 4px 12px -2px rgba(0,0,0,0.75); box-shadow: 2px 4px 12px -2px rgba(0,0,0,0.75); } input[type="date"]{ border:1px solid green; border-radius:0px; } input[type="date"]:hover{ border:1px solid red; border-radius:2px; } </style> </head> <body> <div class="col-md-3"></div> <div class="col-md-6 well"> <h3 class="text-success">Get Hours in Two Differents Dates</h3> <hr> <div class="col-md-2"></div> <div class="col-md-8"> <form action="" method="POST"> <div class="form-group"> <label>Start Of Date</label> <input type="date" name="start" class="form-control"/> </div> <div class="form-group"> <label>End Of Date</label> <input type="date" name="end" class="form-control"/> </div> <button class="btn btn-success btn-block" name="detail">Get Detail</button> </form> <?php include 'calculate.php'?> </div> </div> </body> </html>
Creating PHP Function To Calculate The Exact Value
in this step we are going to create PHP Function to get the exact value. first function is strtotime() anssecond is abs(). here the source code you can save it as calculate.php in your project folder.
<?php function differenceIn_TwoDates($startOf_Date,$endOf_Date){ $timestamp_start = strtotime($startOf_Date); $timestamp_end = strtotime($endOf_Date); $difference_Dates = abs($timestamp_end - $timestamp_start)/3600; return $difference_Dates; } if(ISSET($_POST["detail"])) { $start = strtotime($_POST['start']); $end = strtotime($_POST['end']); $calculation = abs($end - $start)/3600; echo "<center><h3>There are <b style='color:green;'>".$calculation."</b> Hours Different Between Two Dates</h3></center>"; } ?>
If you facing any type of problem with this source code then you can Download the Complete source code in zip Formate by clicking the below button Download Now otherwise you can send Comment.