Re: Date Calculation

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

 



-------------- Original message from Mike Tuller <php@xxxxxxxxxxxxxxxxxx>: -------------- 

> Ok, so here is what I have. Please check to see if there is a better 
> way. There are a lot of database calls to me. 

There are.  Do it in one query.
> 
> $query = "SELECT * FROM hardware_assets WHERE ethernet_address = 
> '$ethernet_address'"; 
> $result = mysql_query($query, $db_connect) or die (mysql_error()); 
> 
> while ($row = mysql_fetch_array( $result )) 
> { 
> $timestamp = $row["script_ran"]; 
> $timequery = "SELECT SEC_TO_TIME(UNIX_TIMESTAMP() - 
> UNIX_TIMESTAMP('$timestamp')) as diff"; 
> $timeresult = mysql_query($timequery, $db_connect) or die 
> (mysql_error()); 
> $time = mysql_result($timeresult, 0, 0); 
> 
> $secondquery = "SELECT TIME_TO_SEC('$time')"; 
> $secondresult = mysql_query($secondquery, $db_connect) or die 
> (mysql_error()); 
> $seconds = mysql_result($secondresult, 0, 0); 
> 
> echo $seconds; 
> } 
> 

I don't know why you're doing both TIME_TO_SEC and SEC_TO_TIME, so I'm leaving them out as they are inverses.  But I'm sure you can figure out what you want.

$query = "SELECT  *, (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(script_ran)) as seconds FROM hardware_assets WHERE ethernet_address =  '$ethernet_address'"; 

This will return all the columns from the matching rows, plus an extra field called seconds that is the difference in seconds between now and wehn the script ran.  

Here is the list of mysql date/time functions for refernece 
http://dev.mysql.com/doc/mysql/en/Date_and_time_functions.html#IDX1454

-- 
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