-1

How to get the total number of monts, days, and minutes from a given minute. Say for example given a value in minutes 93366 should return 2 months ,5 days and 5 hours. This is what I've tried so far.

function convert_minutes($minutes, $output) {
    if ($minutes >= 43833) {
        $whole_month = 0;
        $decimal_month = 0;
        $label = "";
        $months = $minutes / 43833;
        list($whole_month, $decimal_month) = sscanf($months, '%d.%d');
        if ($months > 1) {
            $label = "months";
        } else {
            $label = "month";
        }
        $output .= $months . " " . $label;
        $decimal_month = "0." . $decimal_month;
        if ($decimal_month != 0) {
            return $this->convert_minutes($decimal_month, $output);
        } else {
            return $output;
        }
    } elseif ($minutes >= 1440) {
        $whole_day = 0;
        $decimal_day = 0;
        $label = "";
        $days = $minutes / 1440;
        list($whole_day, $decimal_day) = sscanf($days, '%d.%d');
        if ($days > 1) {
            $label = "days";
        } else {
            $label = "day";
        }
        $output .= $days . " " . $label;
        $decimal_day = "0." . $decimal_day;
        if ($decimal_day != 0) {
            return $this->convert_minutes($decimal_day, $output);
        } else {
            return $output;
        }
    } elseif ($minutes >= 60) {
        $whole_minutes = 0;
        $decimal_minutes = 0;
        $label = "";
        $min = $minutes / 60;

        list($whole_minutes, $decimal_minutes) = sscanf($min, '%d.%d');
        if ($min > 1) {
            $label = "minutes";
        } else {
            $label = "minute";
        }
        $output .= $min . " " . $label;
        $decimal_minutes = "0." . $decimal_minutes;
        if ($decimal_minutes != 0) {
            return $output . " and " . $decimal_minutes . " minutes";
        } else {
            return $output;
        }
    }
}

EDIT

I just wanted to get the estimate. Assuming 1 hour is 60 minutes and 1 day is 1440 minutes and 1 month is 43,200. I am developing a document tracking system and would just like to calculate how long a document stayed in a particular office based on the date received and date released.

beginner
  • 2,024
  • 5
  • 43
  • 76
  • I just wanted to get the estimate. Assuming 1 hour is 60 minutes and 1 day is 1440 minutes and 1 month is 43,200. I am developing a document tracking system and would just like to calculate how long a document stayed in a particular office. – beginner Sep 13 '18 at 01:57
  • 1
    I've started to debug this for you. Your issue starts here, `convert_minutes($decimal_month, $output)`;. `$decimal_month` is the fractional amount of months left but your function is expecting minutes. – user3783243 Sep 13 '18 at 02:12
  • 2
    [similar in javascript](https://stackoverflow.com/a/36099084/2815635) – Niklesh Raut Sep 13 '18 at 03:27

1 Answers1

1

You can use floor and mod operator.
Floor rounds down a number.
The modulo operator will give you what is left if split evenly.

Example 5%2 = 1
Since 2*2 = 4 and the remaining is 1.

Echo floor(93366/43200) . " months\n";
$rem = 93366%43200;
Echo floor($rem/1440) . " days\n";
$rem = $rem%1440;
Echo floor($rem/60) . " hours\n";
Echo $rem%60 . " minutes";

Output:

2 months
4 days
20 hours
6 minutes

https://3v4l.org/RreDY

Andreas
  • 23,610
  • 6
  • 30
  • 62