Current Location: Home> Latest Articles> How to get the current time in combination with the time function

How to get the current time in combination with the time function

gitbox 2025-05-26

In PHP programming, sometimes we need to get the current Greenwich Standard Time (GMT), also known as Coordinated Universal Time (UTC). PHP provides a very convenient combination of methods: the gmdate() function and the time() function can easily achieve this requirement.

1. Introduction to gmdate() and time()

  • gmdate() : Similar to date() , but returns GMT time, not local time.

  • time() : Returns the current timestamp since the Unix Era (00:00:00 GMT on January 1, 1970).

By combining these two functions, you can get the current GMT time.

2. Code examples

 <?php
echo gmdate("Y-m-d H:i:s", time());
?>

This code will output a GMT time string similar to the following format:

 2025-05-22 07:45:30

3. Parameter description

  • "Ymd H:i:s" is a commonly used date and time format in PHP, representing year-month-date hours: minutes: seconds.

  • time() returns the current Unix timestamp.

  • gmdate() receives this timestamp and formats the time according to the GMT standard.

4. Practical application scenarios

  1. Cross-time zone data record : In international applications, the unified use of GMT time helps avoid data confusion caused by time zone differences.

  2. Log system : Many system logs use GMT time format, which facilitates unified processing and comparison of the system.

  3. API timestamp requirements : Some APIs require timestamps to be GMT time, especially during secure signature verification.

5. Compare date() and gmdate()

Some developers are prone to confuse date() and gmdate() . The key difference between the two is the time zone:

 <?php
echo "Local time: " . date("Y-m-d H:i:s") . "\n";
echo "GMT time: " . gmdate("Y-m-d H:i:s") . "\n";
?>

If your server time zone is set to East Eighth District (such as China), the output may be as follows:

 Local time: 2025-05-22 15:45:30
GMT time: 2025-05-22 07:45:30

6. Additional: Get millisecond time (with GMT)

Although gmdate() does not directly support milliseconds, it can be implemented through microtime() splitting. For example:

 <?php
$micro = microtime(true);
$seconds = floor($micro);
$milliseconds = round(($micro - $seconds) * 1000);
echo gmdate("Y-m-d H:i:s", $seconds) . ".$milliseconds GMT";
?>

The output is similar:

 2025-05-22 07:45:30.123 GMT

7. Tips

  • If the PHP program you deploy needs to use GMT time uniformly, you can set the default time zone to UTC:

 <?php
date_default_timezone_set('UTC');
?>

However, using date() at this time will also return GMT time, so you no longer need to use gmdate() .

8. Conclusion

By combining gmdate() and time() , you can get the current GMT time very conveniently in PHP. This method is very practical in cross-time zone processing, unified logging and server-side development. This is undoubtedly a skill worth mastering for PHP developers who need precise control of time output.

For more practical code references, please visit:
https://gitbox.net/snippets/php-gmtime