Home  >  Article  >  Backend Development  >  How to use PHP date() function to convert timestamp to date format

How to use PHP date() function to convert timestamp to date format

PHPz
PHPzOriginal
2023-03-29 16:16:43841browse

In PHP programming, we often need to convert timestamp to date format. The timestamp represents the number of seconds that have passed since 00:00:00 on January 1, 1970, and is a universal time representation method. In PHP, you can use the date() function to convert timestamp to date format.

Assume there is a timestamp variable $timestamp, we can use the following code to convert it to date format:

$date = date('Y-m-d H:i:s', $timestamp);

Among them, the first parameter of the date() function is the date format character String, the second parameter is the timestamp. The following are several commonly used date formatting characters:

  • Y: four-digit year
  • m: two-digit month
  • d: two-digit day
  • H: hour in 24-hour format
  • i: minute
  • s: second

Different date formats can be obtained by combining different characters. For example, 'Y-m-d' means year-month-day, and 'H:i:s' means hour:minute:second.

In addition to the date() function, PHP also provides the gmdate() function and strftime() function to generate date format. gmdate() is similar to the date() function, except that it returns UTC time. The strftime() function supports more localized date formatting options, but is relatively slow.

The following is some sample code:

// 时间戳转换为标准格式日期
$timestamp = 1633283635;
$date = date('Y-m-d H:i:s', $timestamp);
echo $date;  // 输出:2021-10-03 19:40:35

// 时间戳转换为UTC时间
$date = gmdate('Y-m-d H:i:s', $timestamp);
echo $date;  // 输出:2021-10-03 11:40:35

// 使用strftime函数生成本地化日期格式
setlocale(LC_TIME, 'en_US.utf8'); // 指定本地化参数
$date = strftime('%A %B %d %Y', $timestamp);
echo $date; // 输出:Sunday October 03 2021

It should be noted that PHP's timestamp is calculated in seconds. If you need to be precise to milliseconds, you can use the microtime() function to get the decimal part of the current timestamp.

// 获取当前时间的毫秒数
list($usec, $sec) = explode(' ', microtime());
$timestamp = (float) $usec + (float) $sec;
$milliseconds = round($timestamp * 1000); // 取整并转换为毫秒
echo $milliseconds;

The above is the introduction and sample code about converting timestamp to date format in PHP. In actual development, conversion of timestamp and date formats is a very common operation. Developers can choose different functions and date formatting options according to their own needs.

The above is the detailed content of How to use PHP date() function to convert timestamp to date format. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn