How to convert minutes to hours in PHP?

Member

by libby , in category: PHP , 2 years ago

How to convert minutes to hours in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by yadira.tillman , 2 years ago

@libby you can use the simple function below to convert minutes to hours in PHP and the algorithms it's pretty much just dividing minutes by 60:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php

function minToHours(int $min): int {
    return intval($min / 60);
}

// Output: 1
echo minToHours(60);

// Output: 3
echo minToHours(180);


by jerald.kuvalis , a year ago

@libby You can also use the intdiv() function to get the quotient of the division and discard the remainder. look into the code:


1
2
3
4
5
6
7
8
<?php

$minutes = 120;

$hours = intdiv($minutes, 60);

echo $hours;
// Output: 2