Home > Date Time, PHP > Time Difference between times ( start time and end time )

Time Difference between times ( start time and end time )

// an START time value
$start = ’09:00′;
// an END time value
$end = ’10:30′;

// what is the time difference between $end and $start?
if( $diff=@get_time_difference($start, $end) )
{
echo “Hours: ” .
sprintf( ‘%02d:%02d’, $diff[‘hours’], $diff[‘minutes’] );
}
else
{
echo “Hours: Error”;
}

//Call function for time diff

function get_time_difference( $start, $end )
{ $uts[‘start’] = strtotime( $start );
$uts[‘end’] = strtotime( $end );
if( $uts[‘start’]!==-1 && $uts[‘end’]!==-1 )
{
if( $uts[‘end’] >= $uts[‘start’] )
{
$diff = $uts[‘end’] – $uts[‘start’];
if( $days=intval((floor($diff/86400))) )
$diff = $diff % 86400;
if( $hours=intval((floor($diff/3600))) )
$diff = $diff % 3600;
if( $minutes=intval((floor($diff/60))) )
$diff = $diff % 60;
$diff = intval( $diff );
return( array(‘days’=>$days, ‘hours’=>$hours, ‘minutes’=>$minutes, ‘seconds’=>$diff) );
}
else
{
trigger_error( “Ending date/time is earlier than the start date/time”, E_USER_WARNING );
}
}
else
{
trigger_error( “Invalid date/time data detected”, E_USER_WARNING );
}
return( false );
}

Advertisements
Categories: Date Time, PHP Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: