user0129e021939232
user0129e021939232

Reputation: 6355

php subtracting two durations and reduce percentage to get final time

I am storing duration values in a database to find out how much capacity I have to do things in a given working week. For example, I have the following inputs to calculate the capacity duration I have in a week from the total duration, the reduced duration (for example lunch times, meetings, toilet breaks etc) and the percentage to reduce from. So for example I have the following values:

$total_duration = 30:30:00; //(37 hours 30 mins 00 secs)
$reduced_duration = 01:00:00; //(1 hour 00 mins 00 secs)
$capacity_percentage = 90%; // capacity percentage for leeway

Therefore what I want to do subtract $total_duration from $reduced_duration and then get the $capacity_percentage % of this total.

I'm trying the following;

    $total_duration = '30:30:00'; //(37 hours 30 mins 00 secs)
    $reduced_duration= '01:00:00'; //(1 hour 00 mins 00 secs)
    $capacity_percentage ='90%'; // capacity percentage for leeway

    $total_duration_hrs = 0;
    $total_duration_mins = 0;
    $total_duration_secs = 0;

    $reduced_duration_hrs = 0;
    $reduced_duration_mins = 0;
    $reduced_duration_secs = 0;

    list($total_duration_hours, $total_duration_minutes, $total_duration_minutes) = explode(':', $total_duration);

    list($reduced_duration_hours, $reduced_duration_minutes, $reduced_duration_seconds) = explode(':', $reduced_duration);

    $total_duration_hrs += (int) $total_duration_hours;
    $total_duration_mins += (int) $total_duration_minutes;
    $total_duration_secs += (int) $total_duration_minutes;

    $reduced_duration_hrs += (int) $reduced_duration_hours;
    $reduced_duration_mins += (int) $reduced_duration_minutes;
    $reduced_duration_secs += (int) $reduced_duration_seconds;

    $totalhrs = ($total_duration_hrs - $reduced_duration_hrs);
    $totalmins = ($total_duration_mins - $reduced_duration_mins);
    $totalsecs = ($total_duration_secs - $reduced_duration_secs);

This gives me the correct total time i.e. 29:30:30. But now i'm bit unsure as to how I would find the percentage time value, would I find 90% of the hours, minutes and seconds or would I do something else? Any ideas guys?

Upvotes: 2

Views: 267

Answers (1)

Sherif
Sherif

Reputation: 11943

It's easier if you do everything in seconds and only convert it to hours, minutes, seconds, when you want to output or read the values. That way your logic becomes simpler and less dependent on state.

Write a function to convert seconds to hours:minutes:seconds

function secondsToFormat($seconds) {
    $diff = (new DateTime)->sub(new DateInterval("PT{$seconds}S"))->diff(new DateTime);
    $h = $diff->format("%h") + $diff->format("%a") * 24;
    return $diff->format("{$h}:%I:%S");
}

Write a function that converts hours:minutes:seconds to seconds

Now you need a function that does the opposite so you can easily convert between the two.

function formatToSeconds($formattedTimeString) {
    list($hours, $minutes, $seconds) = explode(":", $formattedTimeString);
    $seconds += $hours * 60 * 60;
    $seconds += $minutes * 60;
    return $seconds;
}

Write a function to calculate a percentage of the total

function getPercentage($total, $percent) {
    return (int) floor($total / 100 * $percent);
}

Now you can simply do this...

$t = formatToSeconds($total_duration);
$r = formatToSeconds($reduced_duration);
$diff = $t - $r; // difference in seconds
echo secondsToFormat(getPercentage($diff, 90)), " at 90% capacity";

and you would get something like 26:33:00 at 90% capacity

Upvotes: 2

Related Questions