All pastes #972870 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-04-05 19:50 UTC
rendered paste body
/**
 * Calculates the difference between two timestamps.
 *
 * @param integer $start_time
 * @param integer $end_time
 * @return string
 */
function time_difference ( $start_time, $end_time = false )
{
	// Check if we have an end time.
	$end_time	= ( $end_time != false ) ? $end_time : time ( );

	// Subtract the two timestamps.
	$diff	= $end_time - $start_time;

	// Get the remaining hours. 3600 = seconds in one hour.
	$hours	= floor ( $diff / 3600 );
	$diff	= $diff % 3600;

	// Get the remaining minutes. 60 = seconds in one minute.
	$minutes	= floor ( $diff / 60 );
	$diff		= $diff % 60;

	// What's left is the remaining seconds :) 
	$seconds	= $diff;

	// Format it. Return the difference between the two timestamps.
	return	str_pad ( $hours, 2, '0', STR_PAD_LEFT ) . ':' . str_pad ( $minutes, 2, '0', STR_PAD_LEFT ) . ':' . str_pad ( $seconds, 2, '0', STR_PAD_LEFT );
}