note 47850 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 15 Jan 2006 12:48:40 +0000 |
| Subject: | note 47850 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-101849@lists.php.net to get a copy of this message | ||
Note Submitter: rasmussens AT synergisminc DOT com
----
This function allows you to determine the difference HH:MM:SS between two dates. When you subtract
timestamps and end up with small numbers Date() will return incorrect hours. Only the minutes and
seconds will be correct. Of course 3600 would be Jan 1st 1970 1:00:00 am, but the problem is you
still cant get the correct hours from Date() because it will convert it to a GMT time. Here is a
simple function so that you can return the time for smaller numbers:
<?
function GetTime ($timedifference) {
if ($timedifference >= 3600) {
$hval = ($timedifference / 3600);
$hourtime = intval($hval);
$leftoverhours = ($timedifference % 3600);
$mval = ($leftoverhours / 60);
$minutetime = intval($mval);
$leftoverminutes = ($leftoverhours % 60);
$secondtime = intval($leftoverminutes);
$hourtime = str_pad($hourtime, 2, "0", STR_PAD_LEFT);
$minutetime = str_pad($minutetime, 2, "0", STR_PAD_LEFT);
$secondtime = str_pad($secondtime, 2, "0", STR_PAD_LEFT);
return "$hourtime:$minutetime:$secondtime";
}
if ($timedifference >= 60) {
$hourtime = 0;
$mval = ($timedifference / 60);
$minutetime = intval($mval);
$leftoverminutes = ($timedifference % 60);
$secondtime = intval($leftoverminutes);
$hourtime = str_pad($hourtime, 2, "0", STR_PAD_LEFT);
$minutetime = str_pad($minutetime, 2, "0", STR_PAD_LEFT);
$secondtime = str_pad($secondtime, 2, "0", STR_PAD_LEFT);
return "$hourtime:$minutetime:$secondtime";
}
$hourtime = 0;
$minutetime = 0;
if ($timedifference < 0 ) { $secondtime = 0; }
else { $secondtime = $timedifference; }
$hourtime = str_pad($hourtime, 2, "0", STR_PAD_LEFT);
$minutetime = str_pad($minutetime, 2, "0", STR_PAD_LEFT);
$secondtime = str_pad($secondtime, 2, "0", STR_PAD_LEFT);
return "$hourtime:$minutetime:$secondtime";
}
$mytime = GetTime(1101960767 - 1101961767);
echo $mytime;
?>