microtime();

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

microtime();

Post by dull1554 »

here's my code

Code: Select all

$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$tstart = $mtime;
//whole bunch of code.........
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$tend = $mtime;
$totaltime = ($tend - $tstart);
printf ("<center>page was generated in %f seconds</center>", $totaltime);
here's my question, i want to uses my code to get parse time, but i want to display parse time on the bottom of my menu instead of at the bottom of the page, i have to have the second part of the code at the end of my script to get true parse time, is there a way to printf the time above the second bunch of code?!?!?!?!

not a big deal but i really wanted to do it
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

A shot in the dark, but maybe you could do something with ob_start?

(if you've never used it, just so you know, ob=output buffering)
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i'll check it out, thanks
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i tried doing this but it does not work

Code: Select all

$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$tstart = $mtime;

ob_start();
printf ("<center>page was generated in %f seconds</center>", $totaltime); 

$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$tend = $mtime;
$totaltime = ($tend - $tstart);
ob_end_flush();
any ideas???
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

dull1554 wrote:i tried doing this but it does not work

any ideas???
Be more specific on what it did - what do you mean it did not work?

Did it not display? Not display where you wanted it? etc.

Also, if you could provide a link to the rest of the page (phps) we could get more detailed.
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

here's a cool javascript clock if you want to go that way..
just grab the images from the links below...

http://web.developer.tripod.com/elapsed.html

Code: Select all

<table border=0 cellpadding=2 cellspacing=0 bgcolor="">
<tr><td colspan=5><font size=-1>time elapsed:</font></td></tr>
<tr><td align="center" bgcolor="black" height=40><img src="http://web.developer.tripod.com/clock/dg0.gif" name="_mn1"></td>
<td align="center" bgcolor="black" height=40><img src="http://web.developer.tripod.com/clock/dg0.gif" name="_mn2"></td>
<td align="center" bgcolor="black" height=40><img src="http://web.developer.tripod.com/clock/dgc.gif" name="_c"></td>
<td align="center" bgcolor="black" height=40><img src="http://web.developer.tripod.com/clock/dg0.gif" name="_se1"></td>
<td align="center" bgcolor="black" height=40><img src="http://web.developer.tripod.com/clock/dg0.gif" name="_se2"></td></tr>
</table>


<script language="javascript"><!-- start

dg0 = new Image();dg0.src = "http://web.developer.tripod.com/clock/dg0.gif";
dg1 = new Image();dg1.src = "http://web.developer.tripod.com/clock/dg1.gif";
dg2 = new Image();dg2.src = "http://web.developer.tripod.com/clock/dg2.gif";
dg3 = new Image();dg3.src = "http://web.developer.tripod.com/clock/dg3.gif";
dg4 = new Image();dg4.src = "http://web.developer.tripod.com/clock/dg4.gif";
dg5 = new Image();dg5.src = "http://web.developer.tripod.com/clock/dg5.gif";
dg6 = new Image();dg6.src = "http://web.developer.tripod.com/clock/dg6.gif";
dg7 = new Image();dg7.src = "http://web.developer.tripod.com/clock/dg7.gif";
dg8 = new Image();dg8.src = "http://web.developer.tripod.com/clock/dg8.gif";
dg9 = new Image();dg9.src = "http://web.developer.tripod.com/clock/dg9.gif";
dgc = new Image();dgc.src = "http://web.developer.tripod.com/clock/dgc.gif";

hr = 0;
mn = 0;
se = 0;

function dotime()&#123; 
theTime=setTimeout('dotime()',1000);
  if (se > 59)&#123;se=0; mn++;&#125;
  if (mn > 59)&#123;mn=0; hr++;&#125;

  if(hr < 10) &#123;hrs="0"+hr;&#125;
  else &#123;hrs=hr;&#125;
  if(mn < 10) &#123;mns="0"+mn;&#125;
  else &#123;mns=mn;&#125;  
  if(se < 10) &#123;ses="0"+se;&#125;
  else &#123;ses=se;&#125;
  tot=hrs+mns+ses;
  document._mn1.src = 'clock/dg'+tot.substring(2,3)+'.gif';
  document._mn2.src = 'clock/dg'+tot.substring(3,4)+'.gif';
  document._se1.src = 'clock/dg'+tot.substring(4,5)+'.gif';
  document._se2.src = 'clock/dg'+tot.substring(5,6)+'.gif';
  se++;
&#125;
dotime();
//end -->
</script>
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

sorrymaybe i was not using ob_start and such correctly...it just makes the parse time say 0.000000....

heres the entire source of my index.php file, make with what you wish
http://24.95.139.129/dull1554/showindex.php

thanks again
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Hmm, the way I would do it is use Javascript. In the menu, create a little element with a certain id, lets say x_id. Then, at the end of your file, output some javascript that changes the contents of x_id (it may be easier to use a form element) to whatever. That way, you can wait until the page is parsed and output before you have to set the element in your menu.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
happz
Forum Newbie
Posts: 2
Joined: Sat Jan 17, 2004 2:28 pm
Location: Czech Republic
Contact:

Post by happz »

dull1554 wrote:sorrymaybe i was not using ob_start and such correctly...it just makes the parse time say 0.000000....
When you try to print value of $totaltime you're using an unitialisetd var - so $totaltime has value of 0...

IMHO you should try solution as written above - put some HTML object where you want to have that $totaltime shown, and when finishing page, do something like echo "myObject.value = $totaltime;"

Yes, you have to replace myObject with proper statement (document.getEmelentById(), form., etc) and you have it ;-)

happz
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i was hoping that there may be a pure php soultion.....but if i have to do it that way i guess i will....thanks a bunch
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Just had a brainstorm here. What if throughout the page, you don't echo any HTML, but just store it in variables. Then, when the page is parsed and ready to go, you can set your time variable which will change the HTML you've generated, and output all the variables that you've already created. All the work except actually echoing the code will be done while you're timing. This may be quite messy (in fact I'm almost positive it is), but it would work.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
partiallynothing
Forum Commoner
Posts: 61
Joined: Fri Nov 21, 2003 5:02 pm
Location: connecticut, usa

Post by partiallynothing »

Can we have the WHOLE page code. Depending on how it is layed out, pickle's idea may work...
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

Post Reply