php editor Strawberry will introduce to you today how to format a GMT/UTC date/time in PHP. In web development, handling date/time in different time zones is a common need. With appropriate PHP functions, we can easily convert GMT/UTC time to local time and format it for display. Let’s learn how to implement this feature together!
Format GMT/UTC date/time in PHP
Introduction
In php, formatting GMT/UTC dates/times is critical to correctly displaying and handling cross-time zone dates. This article explains how to format a GMT/UTC date/time using PHP's DateTime
class, and the various formatting options available.
DateTime Class
DateTime
class represents a date and time. It can store and manipulate date/time values in time zones such as GMT/UTC. To create a new DateTime
object, you can pass a string or timestamp.
For example, to create an object representing the current GMT time:
$date = new DateTime("now", new DateTimeZone("GMT"));
Format options
DateTime
The class provides several methods for formatting dates/times, including:
f<strong class="keylink">ORM</strong>at()
: Format the date/time using the specified format string. __toString()
: Convert a date/time object to a string, using PHP default format. Format string
format()
The method uses the following format string to specify the output format:
character | describe |
---|---|
a | Abbreviation of day of the week |
A | Day of the week |
b | Abbreviated month name |
B | Month name |
c | ISO 8601 date and time |
d | The number of days in the date (01-31) |
e | Time zone identifier |
F | Month name (January-December) |
g | 12 hour clock (01-12) |
G | 24-hour hour (00-23) |
h | 12 hour clock (01-12) |
H | 24-hour hour (00-23) |
i | Minutes (00-59) |
l | Day of the week |
L | Whether it is a leap year |
m | Month (01-12) |
M | Abbreviated month name |
n | Month (1-12) |
O | Time zone offset (00:00) |
P | Time zone offset (00:00) |
r | RFC 2822 format date/time |
s | Seconds (00-59) |
S | Milliseconds (000-999) |
t | Number of days in a month |
T | current time |
U | UNIX Timestamp |
v | ISO 8601 Date |
w | Day of the week (0-6) |
W | Week of the year (0-53) |
y | Two-digit year (00-99) |
Y | Four-digit year (0000-9999) |
Example
The following example demonstrates how to format a GMT/UTC date/time using the format()
method:
$date = new DateTime("now", new DateTimeZone("GMT")); // Format to ISO 8601 date and time $iso8601Date = $date->format("c"); // Format to RFC 2822 date and time $rfc2822Date = $date->format("r"); //Format to the specified string $customDate = $date->format("Y-m-d H:i:s"); echo "ISO 8601 date and time: $iso8601Date
"; echo "RFC 2822 date and time: $rfc2822Date
"; echo "Custom date and time: $customDate";
Other methods
In addition to the format()
method, the DateTime
class also provides other methods to help format dates/times:
setTimezone()
: Set the time zone. modify()
: Modify the date/time based on the given string. add()
and sub()
: Add or subtract the given amount of time respectively. Best Practices
DateTime
class because it provides consistent and robust date/time handling. The above is the detailed content of PHP formats a GMT/UTC date/time. For more information, please follow other related articles on the PHP Chinese website!