0

行為の時間を小数第 2 位で秒単位で計算してみます。

protected function microtimeFormat($data)
    {
        $duration = microtime(true) - $data;
        $hours = (int)($duration/60/60);
        $minutes = (int)($duration/60)-$hours*60;
        return $seconds = $duration-$hours*60*60-$minutes*60;

    }

このメソッドは $data として開始時刻を取得します...そして int 秒を取得します

たとえば、2 秒を返します。

私は小数点以下2桁で秒を取得しようとしています...

protected function microtimeFormat($data,$format=null,$lng=null)
    {
        $duration = microtime(true) - $data;
        $hours = (float)($duration/60/60);
        $minutes = (float)($duration/60)-$hours*60;
        $seconds = $duration-$hours*60*60-$minutes*60;
        return number_format((float)$seconds, 2, '.', '');
    }

しかし、それは私に 0.00 を短時間返します

4

2 に答える 2

0

このフォームを使用して、秒単位の時間を生成します (例: 1.20 )

$start = microtime(true);
for ($i=0; $i < 10000000; $i++) { 
    # code...
}
$end = microtime(true);

echo "<br>" . $time = number_format(($end - $start), 2);
// We get this: 1.20

PHP の 2 つの関数のパフォーマンスを比較する例:

define( 'NUM_TESTS', 1000000);

$start = microtime(true);

for( $i = 0; $i < NUM_TESTS; $i++)
{
    mt_rand();
}

$end = microtime(true) - $start;
echo 'mt_rand: ' . number_format(($end), 2) . "\n";

$start = microtime(true);

for( $i = 0; $i < NUM_TESTS; $i++)
{
    uniqid();
}

$end = microtime(true) - $start;
echo 'uniqid: ' . number_format(($end), 2) . "\n";
// We get this: mt_rand: 0.12 uniqid: 2.06
于 2016-09-07T15:05:49.027 に答える