Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> PHP

Hàm microtime() trong PHP

Giới thiệu

Hàm microtime() trả về timestamp hiện tại có độ chính xác microsecond (vi giây). Chỉ hoạt động trên hệ thống hỗ trợ gettimeofday() system call.

Cú pháp

microtime(bool $as_float = false): string|float

Tham số

  • as_float (tùy chọn) - false (mặc định): trả về string "msec sec" (microseconds trước, seconds sau). true: trả về float (giây + phần thập phân microsecond).

Giá trị trả về

Mặc định: string "msec sec" (ví dụ: "0.85623100 1539234951"). Với as_float=true: float timestamp chính xác microsecond.

Ví dụ

Ví dụ 1: Mặc định (string)

<?php
echo(microtime());
?>

Kết quả (ví dụ):

0.85623100 1539234951

Ví dụ 2: Đo thời gian thực thi (as_float=true)

<?php
$time_start = microtime(true);
usleep(100);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did nothing in $time seconds\n";
?>

Kết quả (ví dụ):

Did nothing in 0.00016498565673828 seconds