Home > Backend Development > PHP Tutorial > How to Convert Timestamps to Human-Readable Date/Time in PHP?

How to Convert Timestamps to Human-Readable Date/Time in PHP?

DDD
Release: 2024-10-30 04:11:28
Original
371 people have browsed it

How to Convert Timestamps to Human-Readable Date/Time in PHP?

Converting Timestamps to Human-Readable Date/Time in PHP

Problem:

You have a timestamp stored in a PHP session and want to display it as a human-readable date and time. However, the standard PHP date functions, such as strftime and strtotime, have not been successful in this task.

Solution:

To convert a timestamp to a readable date/time in PHP, use the date() function. This function takes the timestamp as an argument and returns a formatted date and time string according to the specified format.

Example:

Here's how to convert a timestamp of 1299446702:

<code class="php">echo date('m/d/Y', 1299446702);</code>
Copy after login

This code will output "12/28/2010", which is the date and time represented by the provided timestamp.

Customization:

You can customize the format of the date and time string by passing different format specifiers to the date() function. Common format specifiers include:

  • m - Month (01-12)
  • d - Day of the month (01-31)
  • Y - Year (nnnn)
  • H - Hour (00-23)
  • i - Minute (00-59)
  • s - Second (00-59)

For example, the following code will output "December 28, 2010 12:00 AM":

<code class="php">echo date('F j, Y g:i A', 1299446702);</code>
Copy after login

The above is the detailed content of How to Convert Timestamps to Human-Readable Date/Time in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template