note 36101 deleted from function.microtime by aidan
| From: | aidan@php.net | Date: | Tue, 28 Sep 2004 12:08:32 +0000 |
| Subject: | note 36101 deleted from function.microtime by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77483@lists.php.net to get a copy of this message | ||
Note Submitter: Brian Grayless
----
// Here's a useful class that will take care of your script timing in seconds/milliseconds.
class Script_Timer
{
function Script_Timer ()
{
return true;
}
function Start_Timer ()
{
define ("TIMER_START_TIME", microtime());
return true;
}
function Get_Time ($decimals=2)
{
// $decimals will set the number of decimals you want for your milliseconds.
// format start time
$start_time = explode (" ", TIMER_START_TIME);
$start_time = $start_time[1] + $start_time[0];
// get and format end time
$end_time = explode (" ", microtime());
$end_time = $end_time[1] + $end_time[0];
return number_format ($end_time - $start_time, $decimals);
}
}
// You can use:
define ("TIMER_START_TIME", microtime());
// in your program config file (or somewhere it will always get set), or you can use this explicitly
where you want to start the timer:
Script_Timer::Start_Timer();
// A constant definition is used so the starting time can be retrieved from anywhere.
// When you want to retrieve the time difference (from start to finish) use:
$total_time = Script_Timer::Get_Time(3); // The $decimals argument is optional and will default
to 2