Re[2]: Date Calculation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

Friday, October 29, 2004, 7:19:09 AM, you wrote:
MT> Ok, so here is what I have. Please check to see if there is a better 
MT> way. There are a lot of database calls to me.

MT> <?
MT> $database_name = "CETechnology";
MT> $host = "server";
MT> $login = "username";
MT> $password = "password";

MT> $ethernet_address = "00:01:93:8e:69:50";


MT> $db_connect = mysql_connect( $host, $login, $password ) or die 
MT> (mysql_error());
MT> $db_select = mysql_select_db($database_name) or die (mysql_error());

MT> $query = "SELECT * FROM hardware_assets WHERE ethernet_address = 
MT> '$ethernet_address'";
MT> $result = mysql_query($query, $db_connect) or die (mysql_error());

MT> while ($row = mysql_fetch_array( $result ))
MT> {
MT> 	$timestamp = $row["script_ran"];
MT> 	$timequery = "SELECT SEC_TO_TIME(UNIX_TIMESTAMP() - 
MT> UNIX_TIMESTAMP('$timestamp')) as diff";
MT> 	$timeresult = mysql_query($timequery, $db_connect) or die 
MT> (mysql_error());
MT> 	$time = mysql_result($timeresult, 0, 0);
	
MT> 	$secondquery = "SELECT TIME_TO_SEC('$time')";
MT> 	$secondresult = mysql_query($secondquery, $db_connect) or die 
MT> (mysql_error());
MT> 	$seconds = mysql_result($secondresult, 0, 0);

MT> 	echo $seconds;
MT> }


?>>

MT> This returns the seconds since the timestamp was set for the row where 
MT> the ethernet address of the computer is equal to $ethernet_address. I 
MT> can then do the math so that I can display the hours or minutes. I can 
MT> even have it so that when the time is too long, it displays in red, or 
MT> bold, or whatever I want.

Do it in one hit :)    timediff comes out in hours:minutes:seconds

$query = "
        SELECT *,
        UNIX_TIMESTAMP() - UNIX_TIMESTAMP(script_ran) as seconds,
        SEC_TO_TIME(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(script_ran)) as timediff
        FROM hardware_assets 
        WHERE ethernet_address = '$ethernet_address'";
        
$result = mysql_query($query, $db_connect) or die (mysql_error());

while ($row = mysql_fetch_array( $result )){
        $seconds = $row["seconds"];
        $timediff = $row["timediff"];
        echo "$seconds = $timediff <br>";
}

-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux