Re: Date Calculation

From: Date: Thu, 28 Oct 2004 21:19:09 +0000
Subject: Re: Date Calculation
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-200580@lists.php.net to get a copy of this message
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. <? $database_name = "CETechnology"; $host = "server"; $login = "username"; $password = "password"; $ethernet_address = "00:01:93:8e:69:50"; $db_connect = mysql_connect( $host, $login, $password ) or die (mysql_error()); $db_select = mysql_select_db($database_name) or die (mysql_error()); $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; } ?> This returns the seconds since the timestamp was set for the row where the ethernet address of the computer is equal to $ethernet_address. I can then do the math so that I can display the hours or minutes. I can even have it so that when the time is too long, it displays in red, or bold, or whatever I want. On Oct 28, 2004, at 3:14 PM, Tom Rogers wrote:
Hi, Friday, October 29, 2004, 4:51:20 AM, you wrote: MT> I have a shell script that inserts the unix date and time that the MT> script ran into a MySql database. For example Thu Oct 28 13:41:33 CDT MT> 2004. I would like to have my web page do the math so that it displays MT> the time since the script ran. How can I do the math so that the web MT> page will display something like the script ran 1500 seconds ago. MT> Thanks, MT> mike You can do something like this (replace date with column name no quotes) $result = mysql_query("SELECT SEC_TO_TIME(UNIX_TIMESTAMP() - UNIX_TIMESTAMP('2004-10-01 13:45:00')) as diff"); -- regards, Tom


« previous php.general (#200580) next »