The function to check the difference between two time is
<?
function checktime($time1, $time2, $ret)
{
// function accepts in time and date seperated by a space. For the start
// and end times and a return type flag.
// The dates are in the format ddmmyyyy.
// The times are in the formay hhmmss.
// The return flag is H for hours, M for mins, S for secs;
if ( empty($time1) || empty($time2) || empty($ret) )
{
echo "No time(s) supplied to function\n<BR>";
return 0;
}
// First split up the time and date.
list($date1, $time1)=explode(" ", $time1);
list($date2, $time2)=explode(" ", $time2);
// Next split out the component parts of the time and date.
$time1=ereg_replace("[0-9]", "", $time1);
$time2=ereg_replace("[0-9]", "", $time2);
$date1=ereg_replace("[0-9]", "", $date1);
$date2=ereg_replace("[0-9]", "", $date2);
$hh1=substr($time1, 0, 2);
$mm1=substr($time1, 2, 2);
$ss1=substr($time1, 4);
$hh2=substr($time2, 0, 2);
$mm2=substr($time2, 2, 2);
$ss2=substr($time2, 4);
$DD1=substr($date1, 0, 2);
$MM1=substr($date1, 2, 2);
$YY1=substr($date1, 4);
$DD2=substr($date2, 0, 2);
$MM2=substr($date2, 2, 2);
$YY2=substr($date2, 4);
// Change these into a timestamp format.
$td1=mktime($hh1, $mm1, $ss1, $MM1, $DD1, $YY1);
$td2=mktime($hh2, $mm2, $ss2, $MM2, $DD2, $YY2);
// Find the difference.
if ( $td1 > $td2 )
{
$dif=$td1-$td2;
}
else
{
$dif=$td2-$td1;
}
// Format the return value base on the ret field.
$ret=substr(strtoupper($ret), 0, 1);
$div=0;
switch($ret)
{
case 'H' : $div=3600;
break;
case 'M' : $div=60;
break;
case 'S' : $div=1;
break;
default : echo "Unknown return format\n<BR>";
return 0;
break;
}
return($dif/$div);
}
// test it works.
$str1="06/06/2002 164500";
$str2="06-06-2002 17:45:00";
echo "the difference between ".$str1." and ".$str2." is ";
echo checktime($str1, $str2, "H");
?>
Its a bit long and I am sure that someone has done a more compact code but this works. So I have been loath to tamper with it.
All you need to do is request the difference is hours and then chack it with an if statement. You can enter the time in any format i.e. dd/mm/yy, ddmmyy, dd|mm|yy. the program can pick them all up.
Mark.