Difference between durations

duration calculator
time duration calculator
date calculator online
time calculator minutes
date calculator between two dates
number of days between two dates excel
minute calculator
macaulay duration formula

I am creating a timesheet whereby it shows expected and actual hours.

The durations are saved like the below

23:15 - 23 hours and 15 mins

25:45 - 25 hours and 45 mins

I need to work out the difference in hours and mins between the two (extra hours worked)

I have tried the below

$acutal=='23:15';
$expected=='25:45';
$start_time = new DateTime("1970-01-01 $acutal:00");
$time = $start_date->diff(new DateTime("1970-01-01 $expected:00"));

This does work, however when the hours are over 24:00 it throws an error (obviously because it's reading it as time)

Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (1970-01-01 25:45:00)

Is there another way to do this?

You could check if the number of hours are greater than 24, and if so, add a day, and remove 24 hours.

$actual='23:15';
$expected='25:45';

$day = 1;
list($hrs, $min) = explode(':', $expected);
if ($hrs > 24) { $day += 1; $hrs -= 24; }

$start_time = new DateTime("1970-01-01 $actual:00");
$time = $start_time->diff(new DateTime("1970-01-$day $hrs:$min:00"));

echo $time->format('%hh %Im');

Output:

2h 30m

Please also note that == is used to compare, not to assign.

You can also change the if ($hrs > 24) by while(), if there is 48 hours or more.


edit

As pointed out by @CollinD, if the time exceed the number of days of the month, it will fail. Here is another solution:

$actual='23:15';
$expected='25:45';

list($hrs, $min) = explode(':', $actual);
$total1 = $min + $hrs * 60;

list($hrs, $min) = explode(':', $expected);
$diff = $min + $hrs * 60 - $total1;

$start_time = new DateTime();
$expected_time = new DateTime();
$expected_time->modify("+ $diff minutes");
$time = $start_time->diff($expected_time);

echo $time->format('%hh %Im');

Date Duration Calculator: Days Between Dates, The Duration Calculator calculates the number of days, months and years between two dates. The difference between the two modified durations is the modified duration of the interest rate swap.

You can do it manually by keeping track of the number of minutes worked - this will be exact and will also allow you to show negative differences.

<?php
// get the difference in H:mm between two H:mm 
function diff_time($actual, $expected) {
    $diff_mins = mins($actual) - mins($expected);

    return format_mins($diff_mins);
}

// convert a HH:mm to number of minutes
function mins($t) {
    $parts = explode(':', $t);

    return $parts[0] * 60 + $parts[1];
}

// convert number of minutes into HH:mm
function format_mins($m) {
    $mins = $m % 60;
    $hours = ($m - $mins) / 60;

    // format HH:mm
    return $hours . ':' . sprintf('%02d', abs($mins));
}

var_dump(diff_time('23:15', '25:45'));
var_dump(diff_time('25:15', '23:45'));

This outputs:

string(5) "-2:30"
string(4) "1:30"

.. first, 2:30 less than expected, for the second 1:30 more than expected.

Macaulay Duration vs. Modified Duration, modified duration, how to calculate them, and the difference between The Macaulay duration is calculated by multiplying the time period by  Duration is a measure of the sensitivity of the price of a bond or other debt instrument to a change in interest rates. A bond's duration is easily confused with its term or time to maturity because they are both measured in years.

You can try using datetime functions but it seems a lot more straightforward to me to treat the times as string, use split or explode to get hours and minutes, convert to integers, get the difference in minutes and convert it back to hours and minutes (integer divide by 60 and remainder).

$t1=explode(':',$expected);
$t2=explode(':',$actual);
$d=60*($t1[0]-$t2[0])+t1[1]-t2[1];
$result=str_pad(floor($d/60),2,'0',STR_PAD_LEFT).':'.str_pad($d%60,2,'0',STR_PAD_LEFT);

What's the Difference Between Duration and Maturity?, In plain English, “duration” means “length of time” while “maturity” denotes “the extent to which something is full grown.” When bond investors talk  Difference Between Duration and Modified Duration. Duration and modified duration are terms that are often encountered in the field of investments, especially, stocks, and bonds. To be an efficient investor, one needs to know the difference between the two. Duration refers to cash flow of any finances.

Calculate Duration Between 2 Times, Easy & Free, There are 2 basic methods to calculate the difference between 2 times. For instance, what is the duration between 9:00am and 5:45pm, the answer is 8.75 hours  DURATION. Plain and simple, duration is the measure of a bond’s sensitivity to changes in interest rates. Complexity increases in the details of various ways duration is calculated. We will quickly outline the calculations but then then circle back and focus on the broader concept and why investors look at duration in conjunction with maturity.

What's the Difference Between 'Maturity' and 'Duration'?, Each measure plays a key role in helping bond investors evaluate interest-rate risk, but duration is the more complex of the two. Duration is the measure of the time taken to complete a work or a time during which something continues. Work, in contrast, is the measure of how much work has been done in a period of time or the measure of the activity involving mental or physical effort done in order to achieve a result.

Difference between Duration and Work, Another difference is that duration can be measured in seconds, hours or days but work is measured in amount for example 1, 2, 3; we normally say that two out of  The Time Duration Calculator will calculate the time that has elapsed/difference between two dates with time.

Comments
  • If you don't care about time zones and such it's probably easiest to just convert to minutes, subtract, and convert back. No need to add the complexity of creating real DateTime style objects.
  • @CollinD - could you provide an example?
  • As a heads-up, I suspect this will run into the same problem you had before if your durations exceed the number of hours in January of 1970.
  • You're right @CollinD, thanks. I've updated the answer to a new way to do this.
  • If you're already converting everything to minutes, it seems like it might be best to just remove the complexity added by using DateTime all-together. Durations are really just scalars with dimension time, so adding temporal context is only asking for weird edge-case trouble imo.
  • Thank you for your interesting comment @CollinD. (I've slightly modified the code). The DateInterval object allows to format, and I've used that to match to the OP original code.
  • @Syscall - your update returns 0h 00m when using these variables $actual='34:50'; $expected='22:00';