How to Convert a Timestamp into a Human-Readable Date/Time Format in PHP?

Linda Hamilton
Release: 2024-11-01 07:29:02
Original
612 people have browsed it

How to Convert a Timestamp into a Human-Readable Date/Time Format in PHP?

Convert Timestamps into Human-Readable Date/Time Format in PHP

When working with timestamps in PHP, it's often useful to convert them into a readable date and time format for display purposes. This article demonstrates how to convert a timestamp from a session variable into a user-friendly representation.

Query:

A user has a timestamp stored in a session (e.g., 1299446702) and wants to know how to convert it into a readable format.

Response:

To convert a timestamp into a readable date/time format in PHP, utilize the date() function. This function requires the timestamp as the first argument and a format string as the second argument.

Here's an example of how to convert the given timestamp (1299446702):

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

Using the m/d/Y format string, the output will be in the following format: MM/DD/YYYY (e.g., 03/12/2011).

Additional Information:

The date() function provides various format options to customize the output. Some commonly used format specifiers include:

  • d - Day of the month (01-31)
  • m - Month number (01-12)
  • Y - Year (e.g., 2011)
  • H - Hour (00-23)
  • i - Minute (00-59)
  • s - Second (00-59)

The above is the detailed content of How to Convert a Timestamp into a Human-Readable Date/Time Format 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template