Home  >  Article  >  Backend Development  >  Convert php timestamp to human readable date

Convert php timestamp to human readable date

WBOY
WBOYOriginal
2024-04-09 11:12:021201browse

In PHP, you can use the date() function to convert a PHP timestamp to a human-readable date: use the date(string $format, int $timestamp = time()) function. Provide the $format parameter to specify the format of the output date. Optionally provide the $timestamp parameter to specify the UNIX timestamp to convert, which defaults to the current time.

Convert php timestamp to human readable date

Convert PHP timestamp to human-readable date

In PHP you can use date( ) Function converts a UNIX timestamp to a human-readable date.

Syntax:

date(string $format, int $timestamp = time())

Parameters:

  • $format: Specify the output date format. For more information, see [PHP Manual](https://www.php.net/manual/en/function.date.php).
  • $timestamp: The UNIX timestamp to convert. The default value is the current time.

Actual case:

<?php
// 获取当前时间戳
$timestamp = time();

// 将时间戳转换为日期字符串
$dateString = date("Y-m-d H:i:s", $timestamp);

// 输出日期字符串
echo $dateString;
?>

Output:

2023-03-07 13:40:15

In addition to using date() function, you can also use the following function:

  • gmdate(): Convert the timestamp to Greenwich Mean Time (GMT) format.
  • strftime(): Use a more advanced format string to format dates.

Tip:

  • If you want to format a time zone specific date, you can use the DateTime class.
  • You can use online tools to generate format strings, such as [PHP Date Format Generator](https://www.php.net/manual/zh/function.date.php).

The above is the detailed content of Convert php timestamp to human readable date. 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