Current Location: Home> Latest Articles> Use gettimeofday and date functions to implement millisecond formatted output

Use gettimeofday and date functions to implement millisecond formatted output

gitbox 2025-05-29

In PHP, we often need to get the current time and format the output. The default date function only supports accurate to seconds. However, in some scenarios, such as logging, performance monitoring, etc., we need accurate to output time to milliseconds. This article will introduce how to combine gettimeofday and date functions to achieve time format output with milliseconds.


1. Introduction to gettimeofday function

gettimeofday is a function that gets the current time, returning the current Unix timestamp and microsecond information. Its return value can be an array or a floating point number, which contains seconds and microseconds.

 print_r(gettimeofday());

Output example:

 Array
(
    [sec] => 1617181723
    [usec] => 123456
    [minuteswest] => 0
    [dsttime] => 0
)

Here sec is the current number of seconds, and usec is the microseconds within the current number of seconds.

2. Combining date and gettimeofday to achieve millisecond format output

PHP's date function can format seconds time stamps, while the millisecond part can be obtained through the gettimeofday function. Here is a simple example code:

 <?php
$time = gettimeofday();
$sec = $time['sec'];      // Number of seconds
$usec = $time['usec'];    // 微Number of seconds
$milliseconds = floor($usec / 1000); // Convert microseconds to milliseconds

// Format the time string
$formattedTime = date('Y-m-d H:i:s', $sec) . '.' . str_pad($milliseconds, 3, '0', STR_PAD_LEFT);

echo $formattedTime;
?>

Example output result:

 2025-05-24 15:43:12.123

Here, str_pad is used to ensure that the milliseconds are always 3 digits, such as adding zeros before less than 3 digits.

3. Detailed code explanation

  • gettimeofday() returns the seconds and microseconds of the current time.

  • Format the seconds using date('Ymd H:i:s', $sec) .

  • Convert the microseconds into milliseconds by dividing by 1000.

  • Use str_pad to ensure that the millisecond part is triple digits.

4. Encapsulated into functions

For the convenience of multiplexing, we can encapsulate it into a function:

 <?php
function getCurrentTimeWithMilliseconds() {
    $time = gettimeofday();
    $sec = $time['sec'];
    $usec = $time['usec'];
    $milliseconds = floor($usec / 1000);
    return date('Y-m-d H:i:s', $sec) . '.' . str_pad($milliseconds, 3, '0', STR_PAD_LEFT);
}

// Example of usage
echo getCurrentTimeWithMilliseconds();
?>

Call getCurrentTimeWithMilliseconds() to get a time string with milliseconds.


Summarize

By combining gettimeofday and date functions, time format output in PHP with milliseconds can be easily implemented. This method does not rely on additional extensions and is compatible, and is suitable for scenarios where precise timestamps are required.


 <?php
function getCurrentTimeWithMilliseconds() {
    $time = gettimeofday();
    $sec = $time['sec'];
    $usec = $time['usec'];
    $milliseconds = floor($usec / 1000);
    return date('Y-m-d H:i:s', $sec) . '.' . str_pad($milliseconds, 3, '0', STR_PAD_LEFT);
}

echo getCurrentTimeWithMilliseconds();
?>