#native_company# #native_desc#
#native_cta#

Convert “hours:minutes” to “total_minutes” and back

By Bjrn Brndewall
on January 15, 2001

Version: 1

Type: Function

Category: Calendars/Dates

License: GNU General Public License

Description: The first function here will convert hour:minute strings like “1:45” into the total number of minutes, in this case “105”.
The second function will do the opposite.

# Transform hours like "1:45" into the total number of minutes, "105".
function hoursToMinutes($hours)
{
	if (strstr($hours, ':'))
	{
		# Split hours and minutes.
		$separatedData = split(':', $hours);

		$minutesInHours    = $separatedData[0] * 60;
		$minutesInDecimals = $separatedData[1];

		$totalMinutes = $minutesInHours + $minutesInDecimals;
	}
	else
	{
		$totalMinutes = $hours * 60;
	}

	return $totalMinutes;
}

# Transform minutes like "105" into hours like "1:45".
function minutesToHours($minutes)
{
	$hours          = floor($minutes / 60);
	$decimalMinutes = $minutes - floor($minutes/60) * 60;

	# Put it together.
	$hoursMinutes = sprintf("%d:%02.0f", $hours, $decimalMinutes);
	return $hoursMinutes;
}