Home  >  Article  >  Backend Development  >  How to convert timestamp to time format in php

How to convert timestamp to time format in php

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

A timestamp is a way to represent time, it is the number of seconds since 00:00:00 on January 1, 1970. In many applications, we need to convert timestamps into human-readable time formats for a clearer understanding of time. There are several simple ways to convert timestamp to time in PHP. In this article, we will explore these methods.

Method 1: Use the date() function

The date() function can convert the timestamp into a human-readable time format. The following is the parameter format of the date() function:

date(format, timestamp);

Among them, format is the format parameter and timestamp is the timestamp. format can contain the following types of characters:

  • d: day (01-31)
  • j: day (1-31)
  • m: month (01 -12)
  • n: month (1-12)
  • Y: year (for example: 2021)
  • y: year (for example: 21)
  • H: Hours in 24-hour format (00-23)
  • h: Hours in 12-hour format (01-12)
  • i: Minutes (00-59)
  • s: number of seconds (00-59)
  • a: am or pm
  • A: AM or PM

For example, the following code will timestamp Convert to "month/day/year hour:minute:second am/pm" format:

$timestamp = time();
$date = date('m/d/Y h:i: s a', $timestamp);
echo $date;

Output: 07/28/2021 11:53:45 am

Method 2: Use strftime() function

The strftime() function is used to format local dates and times, and can convert timestamps into human-readable time formats. The following is the parameter format of the strftime() function:

strftime(format, timestamp);

Same as the date() function, format and timestamp are function parameters. The format contains the following types of characters:

  • %d: The day of the month
  • %m: The month (01-12)
  • %y: The last two digits of the year (for example: 21)
  • %Y: The complete year (for example: 2021)
  • %H: The hour (00-23)
  • %l: Number of hours, 12-hour format (01-12)
  • %M: Number of minutes (00-59)
  • %S: Number of seconds (00-59)
  • %p: am or pm (uppercase or lowercase)

For example, the following code converts the timestamp to the "month/day/year hour:minute:second am/pm" format:

$timestamp = time();
$date = strftime('%m/%d/%Y %l:%M:%S %p', $timestamp);
echo $ date;

Output: 07/28/2021 11:53:45 am

Method 3: Using the DateTime class

The DateTime class in PHP provides a simple interface to handle dates and times. The createFromFormat() method in the DateTime class can convert the timestamp into a human-readable time format.

The following is the parameter format of the createFromFormat() method:

createFromFormat(format, timestamp);

Among them, format is the string format, if it needs to be converted to the time format If it is different from the input time format, you need to specify the string format. If you need to convert the timestamp into the standard "year-month-day" format, the format should be "Y-m-d H:i:s". timestamp is the timestamp to be converted.

For example, the following code uses the createFromFormat() method to convert a timestamp to a standard time format:

$timestamp = time();
$date = DateTime::createFromFormat('Y-m-d H:i:s', date('Y-m-d H:i:s', $timestamp));
echo $date->format('Y-m-d H:i:s');

Output: 2021-07-28 11:53:45

Conclusion

In this article, we introduced three methods to convert timestamps into human-readable time formats. We can use the date() function and strftime() function in PHP, or use the DateTime class in PHP. Choose the appropriate method for handling timestamp conversions based on your application's needs and personal preferences.

The above is the detailed content of How to convert timestamp to time format in 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