This page was executed in this time...

I've been quite curious for a while on how sites like phpmyadmin it says things like "SQL-query executed in .0095 seconds" etc. and on PHP pages things like "Page rendered in .94 seconds".

Also, when pages say "Executed 8 queries, and a total of 980 queries".

Just wondering how to do simple little footer details that might spruce up a site footer.

Thanks in advance.
Who is Participating?
paStiSSetConnect With a Mentor Commented:
test this one (it works succesfully):

function getmicrotime() {  
    list($usec, $sec) = explode(" ",microtime());  
    return ((float)$usec + (float)$sec);  
$start_time = getmicrotime();

you can put this anywhere (header its a good idea)

$end_time = getmicrotime();
$total_time = round($end_time - $start_time,3);  # the "3" returns: X.XXX, if put "2" this will return X.XX
echo "page generated in $total_time seconds";

and this other one prints the secons you talking about

this should work

$start = microtime();
$starta = explode(" ", $start);
$start = $starta[1] + $starta[0];
your content here
$end = microtime();
$enda = explode(" ", $end);
$end = $enda[1] + $end[0];
$overall = $end - $start;
print "Page rendered in " . $overall . " microseconds";
ASCII_ManConnect With a Mentor Commented:
The option from paStiSSet will work a jiffy for page execution time. As for the number of SQL queries, I alias the mysql_query function for that:
function query($sql){
      global $sql_num_queries, $sql_num_rows, $sql_num_fields, $sql_time;

            $num_queries = 1;

      $start_query_time = getmicrotime();
      $query = mysql_query($sql);
      $end_query_time = getmicrotime();

            $sql_num_rows = mysql_num_rows($query);
            $sql_num_rows += mysql_num_rows($query);
            $sql_num_fields = mysql_num_fields($query);
            $sql_num_fields += mysql_num_fields($query);

            $sql_time = ($end_query_time - $start_query_time);
            $sql_time += ($end_query_time - $start_query_time);

Then embed something more like this at the end of your page:
$total_page_time = round(getmicrotime() - $start_time, 3);
$total_sql_time = round($sql_time, 3);
echo <<<END
 Page generated in $total_page_time seconds
 Read $sql_num_fields within $sql_num_rows rows from $sql_num_queries queries in $total_sql_time seconds

You can add all sorts of useless but interisting server stats if you're bored enough - current number of visitors, bandwidth usage, server uptime, number of SETI data packets crunched...
ignore my script, its poo
drakkarnoirAuthor Commented:
Thank you so so much everybody for your input.

ASCII_Man, when you said I could calculate bandwith, how would I go about doing that? I've seen it done on cPanelX (like that Bandwith used this month) but how would I go about doing that via PHP?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.