Search code examples
phptimehourminute

How to get minutes in the form of hours days weeks months years PHP


I am struggling very hard to achieve this but the results are not what i am trying to get.The result values are wrong and the colons are not on their proper place when using random minute values(that should generate years : months : weeks : days : hours : minutes) from such as 1500, 2800 .etc

I want to convert Minutes into respective years : months : weeks : days : hours : minutes

What i have tried?

$minutesConverter = function($from,$to,$format='dynamic'){
    $year    = floor($from/525600)    ? floor($from/525600)     : '';
    $Rminutes= $from%525600;
    $month   = floor($Rminutes/43800.048)  ? floor($Rminutes/43800.048)  : '';
    $Rminutes= $from%43800.048;
    $week    = floor($Rminutes/10080)     ? floor($Rminutes/10080)     : '';
    $Rminutes= $from%10080;
    $day     = floor($Rminutes/1440)      ? floor($Rminutes/1440)      : '';
    $Rminutes= $from%1440;
    $hour    = floor($Rminutes/60)        ? floor($Rminutes/60)            : '';
    $minute  = $from%60               ? $from%60               : '';            

    if(!empty($year)  ){ if($year   > 1 ){ $year    = $year.' years';     }else{ $year   = $year.' year';     }  }
    if(!empty($month) ){ if($month  > 1 ){ $month   = $month.' months';   }else{ $month  = $month.' month';   }  }
    if(!empty($week)  ){ if($week   > 1 ){ $week    = $week.' weeks';     }else{ $week   = $week.' week';     }  }
    if(!empty($day)   ){ if($day    > 1 ){ $day     = $day.' days';       }else{ $day    = $day.' day';       }  }
    if(!empty($hour) ){ if($hour    > 1 ){ $hour    = $hour.' hours';     }else{ $hour   = $hour.' hour';     }  }
    if(!empty($minute)){ if($minute > 1 ){ $minute  = $minute.' minutes'; }else{ $minute = $minute.' minute'; }  }  

    if(!empty($year))  { $year   = $year;   }
    if(!empty($month)) { if(empty($year)){ $month  = $month; }else{ $month  = ' : '.$month; } }
    if(!empty($week)) { if(empty($month)){ $week  = $week; }else{ $week  = ' : '.$week; } }
    if(!empty($day)) { if(empty($week)){ $day  = $day; }else{ $day  = ' : '.$day; } }
    if(!empty($hour)) { if(empty($day)){ $hour  = $hour; }else{ $hour  = ' : '.$hour; } }
    if(!empty($minute)) { if(empty($hour)){ $minute  = $minute; }else{ $minute  = ' : '.$minute; } }
    
    return $year.$month.$week.$day.$hour.$minute;
};

Example:

For using minute value as 131400 i,e 3 Months(according to google calculator) But the output of this is:

2 months6 hours

Problems?

  1. No colons

  2. Wrong Readable time

Help Needed ! Kindly take a look.


Solution

  • The main issue with your code is that you should be subtracting the number of periods times the period length from $from on each pass, not taking the modulus of $from with the period length. This is because not all your periods are multiples of all the smaller ones (e.g. 1 month is not an exact multiple of weeks). The code can also be signficantly simplified by the use of an array of periods, for example:

    $minutesConverter = function($from) {
        if (!$from) return '0 minutes';
        $periods = array('year' => 525600,
                         'month' => 43800,
                         'week' => 10080,
                         'day' => 1440,
                         'hour' => 60,
                         'minute' => 1);
        $output = array();
        foreach ($periods as $period_name => $period) {
            $num_periods = floor($from / $period);
            if ($num_periods > 1) {
                $output[] = "$num_periods {$period_name}s";
            }
            elseif ($num_periods > 0) {
                $output[] = "$num_periods {$period_name}";
            }
            $from -= $num_periods * $period;
        }
        return implode(' : ', $output);
    };
    
    echo $minutesConverter(131390, 0);
    

    Output:

    2 months : 4 weeks : 2 days : 9 hours : 50 minutes
    

    Demo on 3v4l.org