Home >Backend Development >PHP Problem >How to convert timestamp to date format using PHP

How to convert timestamp to date format using PHP

PHPz
PHPzOriginal
2023-03-21 14:52:561756browse

PHP is a powerful programming language that can not only complete the development of websites and applications, but also supports various data operations and processing. In the actual development process, we often need to convert the timestamp into a more readable date format. Let us learn how to use PHP to convert timestamp to date format.

What is a timestamp?

The timestamp refers to the number of seconds from January 1, 1970 00:00:00 GMT to the current time. It is the most commonly used time representation in computer systems because it is relatively simple to perform time operations based on timestamps.

In PHP, we can use the time() function to get the current timestamp. The example is as follows:

$timestamp = time();
echo $timestamp;

The above code will output the current timestamp, for example: 1620230894.

PHP's method of converting timestamps into date format

PHP provides a variety of ways to convert timestamps into date format. We can choose the appropriate method according to our needs.

  1. Use the date() function to convert the timestamp into date format

The date() function is one of the commonly used functions in PHP for processing date and time. It can Format the timestamp into a date string in the specified format. The following are some common date format parameters:

  • Y: Year, four digits
  • m: Month, with leading zeros (01~12)
  • n : Month, without leading zeros (1~12)
  • d: Date, with leading zeros (01~31)
  • j: Date, without leading zeros (1~31)
  • H: Hours, 24 system, with leading zeros (00~23)
  • h: Hours, 12 system, with leading zeros (01~12)
  • i: Minutes, with Leading zeros (00~59)
  • s: Seconds, with leading zeros (00~59)
  • A: Uppercase morning or afternoon (AM or PM)
  • a: Lowercase morning or afternoon (am or pm)
  • w: Day of the week (0~6)

The sample code is as follows:

$timestamp = 1620230894;
$date = date('Y年m月d日 H:i:s', $timestamp);
echo $date;

The above code will output the date format corresponding to the timestamp, for example: 14:48:14 on May 5, 2021.

  1. Use the strftime() function to convert the timestamp into a date format

The strftime() function is similar to the date() function and can format the timestamp into a specified format. date string. The difference is that the strftime() function supports more localized date and time formatting options. The following are some commonly used localized date and time formatting options:

  • %Y: Year, four digits
  • %m: Month, with leading zeros (01~12)
  • %B: Month, complete name (for example: January~December)
  • %d: Date, with leading zeros (01~31)
  • %A: Day of the week , the complete name (for example: Sunday~Saturday)
  • %H: hour, 24 system, with leading zero (00~23)
  • %M: minute, with leading zero (00~ 59)
  • %S: Number of seconds, with leading zeros (00~59)

The sample code is as follows:

setlocale(LC_ALL, 'chs'); // 设置本地化信息
$timestamp = 1620230894;
$date = strftime('%Y年%m月%d日 %A %H:%M:%S', $timestamp);
echo $date;

The above code will output the timestamp corresponding to Chinese date format, for example: Wednesday, May 05, 2021 14:48:14.

Summary

Converting timestamps to date format is one of the very basic operations in PHP development. This article introduces two commonly used methods: using the date() function and using the strftime() function. Readers can choose the appropriate method according to actual needs to implement the function of converting timestamps into date format. At the same time, it is recommended that readers learn more about PHP date and time processing functions, such as strtotime() function, DateTime class, etc. This knowledge will be helpful in your work in PHP development.

The above is the detailed content of How to convert timestamp to date format using PHP. 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