psubbiah93@gmail.com

Wednesday, 18 November 2015

Calculating Hours Difference in PHP


HTML Datetime Input

<form id="frmDate" action="" method="post">
 <div>
 <label style="padding-top:20px;">Start Date</label><br/>
 <input type="text" name="startdate" value="<?php if(!empty($_POST["startdate"])) { echo $_POST["startdate"]; } ?>" class="demoInputBox">
 </div>
 
 <div>
 <label>End Date</label>
 <span id="userEmail-info" class="info"></span><br>
 <input type="text" name="enddate" value="<?php if(!empty($_POST["startdate"])) { echo $_POST["enddate"]; } ?>" class="demoInputBox">
 </div>
 
 <div>
 <input type="submit" name="submit" value="Find Difference" class="btnAction">
 </div>
</form>

PHP Function to Calculate Hours Difference

<?php
function differenceInHours($startdate,$enddate){
 $starttimestamp = strtotime($startdate);
 $endtimestamp = strtotime($enddate);
 $difference = abs($endtimestamp - $starttimestamp)/3600;
 return $difference;
}
if(!empty($_POST["submit"])) {
 $hours_difference = differenceInHours($_POST["startdate"],$_POST["enddate"]); 
 $message = "The Difference is " . $hours_difference . " hours";
}
?>

Download

Unknown Web Developer

Hello! I am Subbiah P and this is my web technologies blog. My specialities are PHP, Asp.net, jQuery, and CMS web application development. I do accept paid work. Write to me to start working on your dream project . I am Working as Web developer in Peace Soft Technologies

No comments:

Post a Comment