"Roman" <[EMAIL PROTECTED]> wrote:
> I have one problem with mysql database. In table i have 2 arrays with time
> format.
> For example this arrays calls IN and OUT. In php script i want to have
> distinction between
> this arrays. For example IN is 8:30:45 and OUT is 16:45:15 and result
> will be: 8:14:30. Exists any function which do this. Tnanx.

If your MySQL fields are a datetime type (YYYY-MM-DD hh:mm:ss) then you can
convert them to seconds since epoch (Jan 1, 1970 at midnight) and then
calculate the difference and divide by a number to get appropriate units.
Below is a set of functions I wrote to do that.  Before I get to that here's
a simpler solution:

// $time = "16:45:15";
$time_array = explode( ":", $time);
// $time[0] = 16, $time[1] = 45, $time[2] = 15
$time_since_midnight = $time[0] * 3600 + $time[1] * 60 + $time[2];
// Do this conversion for both times, subtract and convert units.

function get_mysql_to_epoch( $date )
    {
    list( $year, $month, $day, $hour, $minute, $second ) = split(
"([^0-9])", $date );
    return date( "U", mktime( $hour, $minute, $second, $month, $day,
$year ) );
    }

function get_elapsed_time( $time_start, $time_end, $units = "seconds",
$decimals = "2" )
    {
    $divider[years]   = ( 60 * 60 * 24 * 365 );
    $divider[months]  = ( 60 * 60 * 24 * 365 / 12 );
    $divider[weeks]   = ( 60 * 60 * 24 / 7 );
    $divider[days]    = ( 60 * 60 * 24 );
    $divider[hours]   = ( 60 * 60 );
    $divider[minutes] = ( 60 );
    $divider[seconds] = 1;

    $elapsed_time = ( ( get_mysql_to_epoch( $time_end ) -
get_mysql_to_epoch( $time_start ) )
                      / $divider[$units] );
    $elapsed_time = sprintf( "%0.{$decimals}f", $elapsed_time );

    return $elapsed_time;
    }

<?php
// Usage example
$time_in  = "2000-06-14 06:30:00";
$time_out = "2000-12-22 13:45:00";
// Full use of function.
echo "<br>Days: ";
echo get_elapsed_time( $time_in, $time_out, "days", "3" );
// Use defaults for units and decimal places.
echo "<br>Seconds: ";
echo get_elapsed_time( $time_in, $time_out );
?>

--
Steve Werby
COO
24-7 Computer Services, LLC
Tel: 804.817.2470
http://www.247computing.com/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to