Skip to content

Instantly share code, notes, and snippets.

@agamm
Last active October 13, 2022 22:26
Show Gist options
  • Save agamm/7a839180075b77220729 to your computer and use it in GitHub Desktop.
Save agamm/7a839180075b77220729 to your computer and use it in GitHub Desktop.
Simple time logging class in PHP.
<?php
//Exec time class
/*
Example:
Time::start('functionToTest');
functionToTest();
Time::p();
Time::t();
*/
class Time{
private static $first = true;
private static $firstTime;
private static $start;
private static $test_name;
static function start($test_name){
self::$start = microtime(true);
self::$test_name = $test_name;
if(self::$first){
self::$first = false;
self::$firstTime = self::$start;
}
}
private static function time_format_ms($time){
$subf = " seconds";
$t = $time;
$subf = " ms";
$t *= 1000;
return $t.' '.$subf;
}
static function p($tf=false){
$time = microtime(true);
if(!$tf){
printf('<br />%s took: %s', self::$test_name, self::time_format_ms($time - self::$start));
}else{
printf('<br />%s took: %.16f seconds', self::$test_name, ($time - self::$start));
}
}
static function t($tf=false){
$time = microtime(true);
if(!$tf){
printf('<br />Total time: %s', self::time_format_ms(($time - self::$firstTime)));
}else{
printf('<br />Total time: %.16f seconds', ($time - self::$firstTime));
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment