Home  >  Article  >  Backend Development  >  PHP formats GMT/UTC time/date according to locale

PHP formats GMT/UTC time/date according to locale

WBOY
WBOYforward
2024-03-21 10:01:01970browse

php editor Xiaoxin introduces you how to format GMT/UTC time/date according to regional settings. In PHP, you can use the date_default_timezone_set() function to set the time zone, and then use the date() function to format the time and date. Time zone settings are important to ensure that the time is displayed correctly and to avoid potential errors. Follow this article to learn how to correctly format GMT/UTC time/date in PHP to make your time processing more accurate and professional.

PHP Format GMT/UTC time and date according to locale

Introduction

php Provides various functions that can be used to format GMT/UTC times and dates according to different locales. This is useful for displaying timestamps based on user location or requirements.

POSIX timestamp

POSIX timestamp is an integer representing the number of seconds since midnight on January 1, 1970. POSIX timestamps in GMT/UTC can be converted to local time or to a locale-specific timestamp.

function:

  • gmdate() - Format a GMT/UTC timestamp as a date/time string.
  • gmmktime() - Convert GMT/UTC time and date to POSIX timestamp.

Time Zone Offset

Time zone offset represents the time difference between a specific time zone and GMT/UTC. This is crucial for adjusting when displaying timestamps in different time zones.

function:

  • timezone_offset_get() - Get the offset of a specific time zone.
  • timezone_name_get() - Get the name of a specific time zone.

regional settings

Regional settings define date, time, currency, and other formatting conventions for a specific region. PHP uses the setlocale() function to set the current locale.

function:

  • setlocale() - Set the current locale.
  • localeconv() - Gets the locale-specific value for the current locale.

Time zone

A time zone is an area on Earth where all locations use the same standard time. PHP uses time zone identifiers to specify different time zones.

function:

  • date_default_timezone_get() - Get the default time zone.
  • date_default_timezone_set() - Set the default time zone.

Format timestamp

When formatting timestamps, you can specify a format string to control the output format. The format string contains the following placeholders:

  • %a - abbreviation for day of the week (for example, Sun)
  • %A - The full name of the day of the week (for example, Sunday)
  • %b - Abbreviation of month (for example, Jan)
  • %B - The full name of the month (for example, January)
  • %C - Year of the century (for example, 20)
  • %d - Day of the month (for example, 01)
  • %e - Day of the month, padded with spaces (for example, 1)
  • %H - hour in 24-hour format (for example, 00)
  • %I - hour in 12-hour format (for example, 00)
  • %j - Day of the year (for example, 001)
  • %m - month number (e.g., 01)
  • %M - Minutes (for example, 00)
  • %p - morning or afternoon (for example, AM)
  • %S - Seconds (for example, 00)
  • %U - Week of the year (e.g., 01)
  • %W - Week of the month (for example, 01)
  • %x - local date representation (for example, 01/01/2023)
  • %X - local time representation (for example, 00:00:00)
  • %y - The last two digits of the year (for example, 23)
  • %Y - The complete year (for example, 2023)

Example

The following examples demonstrate how to format GMT/UTC timestamps based on different locales and time zones:

// Get the current GMT/UTC timestamp
$timestamp = time();

// Format timestamp according to Eastern Time (EST)
setlocale(LC_TIME, "en_US");
$fORMatted_date = gmdate("Y-m-d H:i:s", $timestamp);
echo "EST: $formatted_date
";

// Format timestamp according to Japan Standard Time (jsT)
setlocale(LC_TIME, "ja_JP");
$formatted_date = gmdate("Y year m month d day H hour i minute s second", $timestamp);
echo "JST: $formatted_date
";

Output:

EST: 2023-12-28 14:09:13
JST: December 28, 2023 23:09:13

The above is the detailed content of PHP formats GMT/UTC time/date according to locale. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete
Previous article:PHP draws a dotted lineNext article:PHP draws a dotted line