Re: Need a function to calculate time difference.

From: Date: Wed, 02 Jul 2003 21:52:19 +0000
Subject: Re: Need a function to calculate time difference.
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-153795@lists.php.net to get a copy of this message
Hi, I just went through this... for my daughters drive time for a level one michigan license. Thanks to Wendell Brown for the help. following is my create table command; CREATE TABLE mileage (
           id int(11) NOT NULL auto_increment, 
           drive_date varchar(12) NOT NULL default '', 
           start_time time NOT NULL default '00:00:00', 
           end_time time NOT NULL default '00:00:00', 
           start_miles decimal(6,1) NOT NULL default '0.0', 
           end_miles decimal(6,1) NOT NULL default '0.0', 
           type_of_driving tinytext NOT NULL, 
           PRIMARY KEY  (id) 
         ) TYPE=MyISAM
At 05:11 PM 7/2/2003 -0400, you wrote:
I am trying to find a function or information on how to properly take a start time and an end time from mysql timestamps in order to calculate time taken. So in theory $endtime-$starttime = timespent. It would be great if this understood that 11:55 pm til 12:10am one day apart only equals 15 minutes. Links, example code etc would be great! Thanks
$sql = "Select ID, drive_date, start_time, end_time, start_miles, end_miles, type_of_driving FROM mileage";
        $result = mysql_query( $sql );
        if ( $result != false )
        {
                $drive="Drive date: %s<br>";                                    // Formatting the output
                $sstime="Start Time: %s<br>";
                $setime="End Time: %s<br>";
                $ssmiles="Start Miles: %s<br>";
                $tot_tim="Total Time: %s<br>";
                $semiles="End Miles: %s<br>";
                $tot="Total Miles: %6.1f<br>";
                $tod="Type of Driving: %s<br><br>";
                while ( $data = mysql_fetch_assoc( $result ) )                  // Loading the array
                        {
                                $tot_miles = ($data["end_miles"] - $data["start_miles"]);       //calculate total miles
This is the time calculations section....
                                $sTime = strtotime( $data["start_time"] );
                                $eTime = strtotime( $data["end_time"] );
                                $tot_time = $eTime - $sTime;                    // Elapsed Seconds
                                printf($drive,$data["drive_date"]);
                                printf($sstime,$data["start_time"]); // display start time
                                printf($setime,$data["end_time"]); // display end time
                                printf( $tot_tim,gmstrftime( "%H:%M", $tot_time ) );    // display total time driving
                                printf($ssmiles,$data["start_miles"]); // display start miles
                                printf($semiles,$data["end_miles"]); // display end miles
                                printf( $tot, $tot_miles );                             // display total miles driven
                                printf($tod,$data["type_of_driving"]);
                        }
        } else {
                        echo mysql_error();
                        }
}
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Gilebert/Larry

« previous php.general (#153795) next »