Re: Time problem

From: Date: Thu, 08 Feb 2001 06:28:45 +0000
Subject: Re: Time problem
Groups: php.general 
Request: Send a blank email to php-general+get-38684@lists.php.net to get a copy of this message
Addressed to: "Steve Werby" <steve@247computing.com> "Roman" <rduriancik@indprop.gov.sk> "Php-General" <php-general@lists.php.net> ** Reply to note from "Steve Werby" <steve@247computing.com> Wed, 7 Feb 2001 09:28:29 -0500 Or you could do it the easy way in MySQL: SELECT SEC_TO_TIME( TIME_TO_SEC( OUT ) - TIME_TO_SEC( IN )) AS Hours, .. .. .. This requires IN and OUT to be type TIME. > > "Roman" <rduriancik@indprop.gov.sk> 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: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > > Rick Widmer Internet Marketing Specialists http://www.developersdesk.com

« previous php.general (#38684) next »