Current Location: Home> Latest Articles> Processing formatting of specific dates with gmdate combined with strtotime

Processing formatting of specific dates with gmdate combined with strtotime

gitbox 2025-05-26

In PHP, processing and formatting dates are common requirements in development. gmdate() and strtotime() are two very practical functions that can help us convert time strings to timestamps and output them in GMT (Greenwich Standard Time) format. But how to correctly combine these two functions to format the specified date and time? This article will explain in detail.

1. Introduction to strtotime()

strtotime() is used to parse the date-time description of any English text into a Unix timestamp (i.e., the number of seconds from January 1, 1970). It supports various common date formats such as:

 <?php
$timestamp = strtotime('2025-05-22 15:30:00');
echo $timestamp;
?>

The above code will output an integer timestamp.

2. Introduction to gmdate()

gmdate() is similar to date() , but it outputs Greenwich Standard Time (GMT), not the time of the current server time zone. Its first parameter is the date format string, and the second parameter is the timestamp:

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

This outputs the current GMT time.

3. How to use it in combination

A typical usage of combining these two functions is:

  • Use strtotime() to convert the string date and time to a time stamp

  • Format the timestamp to the desired GMT format string with gmdate()

Example:

 <?php
$input_date = '2025-05-22 15:30:00';
$timestamp = strtotime($input_date);
$formatted_date = gmdate('Y-m-d H:i:s', $timestamp);
echo $formatted_date;
?>

Here, regardless of the server time zone, the specified date and time will be output as GMT time.

4. Things to note

  • The string entered to strtotime() must be in a valid date format, otherwise false will be returned.

  • gmdate() outputs GMT time. If you need local time, you should use date() instead.

  • When combined, make sure that the time zone meaning of the input time string is clear, otherwise time deviation may occur.

5. Advanced usage

Sometimes we want to parse the string time in a specified time zone and then convert it to GMT, which can be combined with the DateTime class:

 <?php
$date = new DateTime('2025-05-22 15:30:00', new DateTimeZone('Asia/Shanghai'));
$date->setTimezone(new DateTimeZone('GMT'));
echo $date->format('Y-m-d H:i:s');
?>

This allows for more flexibility in handling time zone conversion.


The above is how to correctly format the specified date and time in combination with gmdate and strtotime . Mastering it can effectively avoid the problem of time format and time zone and improve the accuracy of date and time processing.


 <?php
// Sample code:Convert the specified time string to GMT Format time output
$input_date = '2025-05-22 15:30:00';
$timestamp = strtotime($input_date);
if ($timestamp === false) {
    echo 'Invalid time format';
} else {
    echo gmdate('Y-m-d H:i:s', $timestamp);
}
?>

 <?php
// use DateTime Classes handle time zones more accurately
$date = new DateTime('2025-05-22 15:30:00', new DateTimeZone('Asia/Shanghai'));
$date->setTimezone(new DateTimeZone('GMT'));
echo $date->format('Y-m-d H:i:s');
?>

If you need to refer to relevant documents, you can access:

https://gitbox.net/manual/en/function.gmdate.php
https://gitbox.net/manual/en/function.strtotime.php
https://gitbox.net/manual/en/class.datetime.php