Re: Date Calculation
| From: | (Jennifer Goodie) | Date: | Thu, 28 Oct 2004 21:42:03 +0000 |
| Subject: | Re: Date Calculation | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-200582@lists.php.net to get a copy of this message | ||
-------------- Original message from Mike Tuller <php@ce.anoka.k12.mn.us>: --------------
> 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