How To Get Hours in Two Different Dates using PHP

0 1,029

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">
		<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">
				border-top:1px dotted #ccc;
				-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);
				border:1px solid green;
				border:1px solid red;
	<div class="col-md-3"></div>
	<div class="col-md-6 well">
		<h3 class="text-success">Get Hours in Two Differents Dates</h3>
		<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 class="form-group">
					<label>End Of Date</label>
					<input type="date" name="end" class="form-control"/>
				<button class="btn btn-success btn-block" name="detail">Get Detail</button>
			<?php include 'calculate.php'?>


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.

	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.

Leave A Reply

Your email address will not be published.