Crop string output?

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
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Crop string output?

Post by JAB Creations »

How can I crop the seconds of the following script so...
Rendered in 0.000921964645386 seconds.

...would be rendered as...
Rendered in 0.00092 seconds.
?

Here is the script...

Code: Select all

<?php
$time_start = microtime(true);
// Sleep for a while
usleep(0);
 
$time_end = microtime(true);
$time = $time_end - $time_start;
 
echo "Rendered in $time seconds.\n";
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Crop string output?

Post by John Cartwright »

number_format(), substr(), sprintf(), etc, etc
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Crop string output?

Post by JAB Creations »

Thanks, I have used substr before though I couldn't remember off-hand.

Code: Select all

<?php
$time_start = microtime(true);
// Sleep for a while
usleep(0);
 
$time_end = microtime(true);
$time = $time_end - $time_start;
 
//echo "Rendered in  $time seconds\n";
echo 'Rendered in ';
echo substr($time, 0, 7);
echo ' seconds.';
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Crop string output?

Post by Chris Corbyn »

round()?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Crop string output?

Post by JAB Creations »

Thanks Chris, the suggestion was closer in context though I'm happy to know about both. :D

Code: Select all

<?php
$time_start = microtime(true);
// Sleep for a while
usleep(0);
 
$time_end = microtime(true);
$time = $time_end - $time_start;
 
//echo "Rendered in  $time seconds\n";
echo 'Rendered in ';
//echo substr($time, 0, 7);
echo round($time, -7);
echo ' seconds.';
?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Crop string output?

Post by s.dot »

You will want the second parameter of round() to be positive. Suppose your script output 0.0090 exactly.

round($time, 5) should do!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Crop string output?

Post by JAB Creations »

I tried that though locally with PHP 5.2.0 I get...
Rendered in 2.19E-005 seconds.
Uh...? :dubious:
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Crop string output?

Post by s.dot »

exponentially fast ;d

same as .0000219
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Crop string output?

Post by JAB Creations »

I'm sorry you lost me?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Crop string output?

Post by John Cartwright »

JAB Creations wrote:I'm sorry you lost me?
It was scottay's attempt at "humor" 8)

FYI, 2.19E-005 === .0000219, which is what he was getting at
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Crop string output?

Post by JAB Creations »

Oh ok, I'm not afraid to admit lots of people know lots of stuff that I don't. :mrgreen:

So is the value technically correct though just not displaying humanly readable or?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Crop string output?

Post by s.dot »

Yes, it is correct. I seriously doubt you'll have any pages that load fast enough to get the E in your time output. of course usleep(0) would do that.. if that's all you have on your page.

do usleep(1000); :P
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Crop string output?

Post by JAB Creations »

So many choices! :rofl:

Though I want the most accurate/honest/context-correct method I can/should use. Here are some err 'benchmarks' I ran locally...

echo substr($time, 0, 7);
0.00046
0.00034
0.00036
echo round($time, 7) && usleep(1);
0.0008402
0.0005021
0.0006831
0.0007231
-1.9E-006
echo round($time, 7) && usleep(1000);
-1.9E-006
0.0156231
0.0573151
0.0014811
0.001811
0.0010831
0.0572259
When I see E does that mean my website is faster then the Enterprise?
Post Reply