Date: 10/14/01
- Next message: Philip Olson: "Re: [PHP] how to set font colors"
- Previous message: Mike Patton: "Re: [PHP] how to set font colors"
- In reply to: Mike Patton: "Re: [PHP] how to set font colors"
- Next in thread: James, Yz: "[PHP] Re: Time Processing"
- Reply: James, Yz: "[PHP] Re: Time Processing"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi,
As I said, PHP doesn't seem to have enough built-in functions to process
time values.
So, I wrote my own functions to deal with times and I'm posting them here
for either inclusion, or for other people's use.
Here they are. Enjoy.
=====================
<?php
function CheckTime($t) {
/* Checks the value of a 24-hour formatted time to be correct.
Returns 0 if bad time format, 1 otherwise.
*/
return ( ereg("[012][0-9]:[0-5][0-9](:[0-5][0-9])?", $t) );
}
function TimeToUnixTime($t) {
/* Takes a time in HH:MM:SS format and returns a unix time (or number of
seconds) since Jan 1, 1970 (UNIX epoch).
Returns 0 if the format is incorrect.
*/
if ( !CheckTime($t) )
return (0);
$time = explode (":", $t);
return ( mktime($time[0], $time[1], $time[2], 1, 1, 1970) - 18000);
/* I have no idea why mktime behaves this way, but I had to deduct 5 hours
to make it work right. As if it's returned in GMT.
*/
}
function SumTimes($t1, $t2) {
/* Takes two times in HH:MM:SS format and returns the sum.
Returns 0 if task is not possible.
*/
if ( !CheckTime($t1) || !CheckTime($t2) )
return (0);
$time1 = explode (":", $t1);
$time2 = explode (":", $t2);
$h = $time1[0] + $time2[0];
$m = $time1[1] + $time2[1];
$s = $time1[2] + $time2[2];
while ( $s > 59 ) {
$m++;
$s -= 60;
}
while ( $m > 59 ) {
$h++;
$m -= 60;
}
return ( sprintf("%02d:%02d:%02d", $h, $m, $s) );
/* another method is to use date() and mktime() instead of all the above, like this:
return( date("H:i:s", (TimeToUnixTime($t1) + TimeToUnixTime($t2) + 18000)) );
*/
}
function DiffTimes($t1, $t2) {
/* Takes two times in HH:MM:SS format and returns the difference.
$t1 must be less than $t2. Returns 0 if task is not possible.
*/
if ( !CheckTime($t1) || !CheckTime($t2) )
return (0);
$time1 = explode (":", $t1);
$time2 = explode (":", $t2);
$h = $time2[0] - $time1[0];
$m = $time2[1] - $time1[1];
$s = $time2[2] - $time1[2];
while ( $s < 0 ) {
$s += 60;
$m--;
}
while ( $m < 0 ) {
$m += 60;
$h--;
}
return( sprintf("%02d:%02d:%02d", $h, $m, $s) );
}
?>
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe <email protected> For additional commands, e-mail: php-general-help <email protected> To contact the list administrators, e-mail: php-list-admin <email protected>
- Next message: Philip Olson: "Re: [PHP] how to set font colors"
- Previous message: Mike Patton: "Re: [PHP] how to set font colors"
- In reply to: Mike Patton: "Re: [PHP] how to set font colors"
- Next in thread: James, Yz: "[PHP] Re: Time Processing"
- Reply: James, Yz: "[PHP] Re: Time Processing"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

