Hướng dẫn calculate hours in php

I have a small issue while calculating sum of total hours

Nội dung chính

  • HTML Datetime Input
  • PHP Function to Calculate Hours Difference
  • How do you calculate the number of hours?
  • How can I calculate hours between two dates in php?
  • How do you total in php?
  • How can I add hours and minutes in php?

My working hours is like:

Day1 - 12:23 Hours
Day2 - 11:43 Hours
Day3 - 10:18 Hours

How can I get it by php? Thanks in advance

Noich

13.6k15 gold badges61 silver badges90 bronze badges

asked Mar 13, 2013 at 12:02

0

Just add them like this:

$day1hours = "12:23";
$day2hours = "11:43";
$day1 = explode(":", $day1hours);
$day2 = explode(":", $day2hours);

$totalmins = 0;
$totalmins += $day1[0] * 60;
$totalmins += $day1[1];
$totalmins += $day2[0] * 60;
$totalmins += $day2[1];

$hours = $totalmins / 60;
$minutes = $totalmins % 60;

$totalhours = "$hours.$minutes";

It is quite elaborate like this, but it just to show the idea: calculate back to minutes, add minutes together, then calculate back to hours.

Note that PHP has quite extensive support for dates and times. Consider using that: http://www.php.net/manual/en/book.datetime.php

Hướng dẫn calculate hours in php

admdrew

3,7254 gold badges25 silver badges39 bronze badges

answered Mar 13, 2013 at 12:06

Bart FriederichsBart Friederichs

32.2k15 gold badges96 silver badges186 bronze badges

This technique will give exact total hours and minutes. The above answer gives me 1.25.15 when i add 00:30 and 00:45

<?php
$hourMin = array("00:30","00:45"); //an Multiple Hour:Minutes array
$hours = 0;
$mins  = 0;
  foreach($hourMin as $val)
  {
      $explodeHoursMins = explode(':',$val);
      $hours += $explodeHoursMins[0];
      $mins  += $explodeHoursMins[1];
  }

  $minToHours =  date('H:i', mktime(0,$mins)); //Calculate Hours From Minutes
  $explodeMinToHours = explode(':',$minToHours);
  $hours += $explodeMinToHours[0];
  $finalMinutes = $explodeMinToHours[1];
  echo $hours." : ".$finalMinutes;
?>

answered Feb 2, 2018 at 11:46

Vishnu BhadoriyaVishnu Bhadoriya

1,6161 gold badge21 silver badges28 bronze badges

If you use this function to calculate time calculation then it will work

function sumTime($initialHour, $finalHour) {
    $h = date('H', strtotime($finalHour));
    $m = date('i', strtotime($finalHour));
    $s = date('s', strtotime($finalHour));
    $tmp = $h." hour ".$m." min ".$s." second";
    $sumHour = $initialHour." + ".$tmp;
    $newTime = date('H:i:s', strtotime($sumHour));
    return $newTime;
 };

For more explanation refer this link: Time Calculation in php

answered Mar 13, 2013 at 12:16

by Vincy. Last modified on July 11th, 2022.

In PHP, there are many ways to calculate the difference between two dates. In this tutorial, we are using PHP date time functions to calculate the hour difference between two dates.

In this example, we are start and end dates from the user. And then, we use strtotime() to convert the date string into timestamp to calculate the difference.

View Demo

HTML Datetime Input

This code shows the input field to get the start and end date from the user.

<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

We call this PHP function when the user submitting the date ranges to calculate the hour 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";
}
?>

View DemoDownload

↑ Back to Top

How do you calculate the number of hours?

How to calculate hours worked.

Determine the start and the end time. ... .

Convert the time to military time (24 hours) ... .

Transform the minutes in decimals. ... .

Subtract the start time from the end time. ... .

Subtract the unpaid time taken for breaks..

How can I calculate hours between two dates in php?

You can convert them to timestamps and go from there: $hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

How do you total in php?

Begin calculating the total cost: $total = $price * $quantity; $total = $total + $shipping; $total = $total - $discount; The asterisk (*) indicates multiplication in PHP, so the total is first calculated as the number of items purchased ($quantity) multiplied by the price.

How can I add hours and minutes in php?

php function CalculateTime($times) { $i = 0; foreach ($times as $time) { sscanf($time, '%d:%d', $hour, $min); $i += $hour * 60 + $min; } if($h = floor($i / 60)) { $i %= 60; } return sprintf('%02d:%02d', $h, $i); } $date[] = '02:32'; $date[] = '01:29'; echo CalculateTime($date); ?>