Home  >  Article  >  Backend Development  >  php timestamp converted to date format

php timestamp converted to date format

PHPz
PHPzOriginal
2024-04-09 13:36:01947browse

How to convert timestamp to date format in PHP? Use the date() function, passing the format string and timestamp. PHP provides predefined format strings such as Y (year), m (month), d (date). Practical case: Display the timestamp in the format of "day of the week, year, month and day", using the date() and strtotime() functions.

php timestamp converted to date format

Convert PHP timestamp to date format

Introduction

Timestamp It is a numerical format that represents a specific point in time and is widely used in computer systems and programming. Sometimes, timestamps need to be converted to a human-readable date format for display to the user or for further processing. This tutorial shows you how to easily accomplish this conversion in PHP.

Using the date() function

The date() function is the most common way to convert a timestamp to date format. Its syntax is as follows:

date(format, timestamp)

Where:

  • format is the format string of the date and time to be converted.
  • timestamp is the timestamp to be converted, defaulting to the current time.

To output a timestamp in a specific format, simply pass the format string to the date() function. For example, the following code converts a timestamp into "year-month-day" format:

$timestamp = time();
$date = date("Y-m-d", $timestamp);
echo $date; // 输出:2023-03-08

Predefined format strings

PHP provides some predefined Format strings, you can use them as needed:

  • Y: four digits representing the year
  • y: two digits Indicates the year
  • m: Two digits indicates the month
  • d: Two digits indicates the date
  • H: Hours (24-hour format)
  • i: Minutes
  • s: Seconds
  • a:AM/PM

Practical Case

Suppose you have a timestamp stored in the database and you want to display it on a web page as " Day of the week, year, month and day”. Here is the code snippet that does this:

$timestamp = strtotime("2023-03-08 12:30:00");
$weekday = date("l", $timestamp);
$date = date("Y-m-d", $timestamp);
echo "$weekday, $date"; // 输出:星期三, 2023-03-08

strtotime() function

strtotime() function can be used to convert a human-readable datetime string to a time stamp. This is complementary to the date() function and can be used to easily convert dates and times in your application.

Conclusion

You can easily convert timestamps in PHP to various date formats by using the date() function and predefined format strings. You can also easily convert datetime strings to timestamps using the strtotime() function. This makes working with dates and times in programming much more convenient.

The above is the detailed content of php timestamp converted 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