Home >Backend Development >PHP Problem >How to convert php timestamp to time format

How to convert php timestamp to time format

PHPz
PHPzOriginal
2023-03-29 11:32:06487browse

In PHP, it is often necessary to convert timestamps (Unix timestamps) into readable time formats. A timestamp is a number of seconds from January 1, 1970 00:00:00 UTC to a certain time, and is a commonly used way to represent time. In PHP, timestamps can be easily converted to various time formats by using the date() function.

The following is a sample code:

<?php
// 定义时间戳
$timestamp = 1608653403;

// 输出时间戳对应的日期格式
echo date(&#39;Y-m-d H:i:s&#39;, $timestamp);
?>

The output result of this code is: 2020-12-23 16:36:43. Among them, the first parameter of the date() function is the output format, and the second parameter is the timestamp variable.

Let’s introduce the common date format parameters:

  • Y: represents the year, expressed in four-digit format (such as: 2021);
  • y : represents the year, expressed in two-digit format (such as: 21);
  • m: represents the month, expressed in two-digit format (such as: 01 represents January, 12 represents December);
  • n: represents the month, expressed in a one-digit format (such as: 1 represents January, 12 represents December);
  • d: represents the date, expressed in a two-digit format (such as: 01 represents 1 day, 31 means the 31st);
  • j: represents the date, expressed in a one-digit format (for example: 1 means the 1st, 31 means the 31st);
  • H: represents the hour , displayed in 24-hour format (for example: 01 represents 1 am, 23 represents 11 pm);
  • h: represents the hour, displayed in 12-hour format (for example: 01 represents 1 am, 11 represents morning 11 o'clock);
  • i: represents minutes, expressed in two-digit format (for example: 01 represents the 1st minute, 59 represents the 59th minute);
  • s: represents seconds, expressed in Two-digit format representation (for example: 01 represents the 1st second, 59 represents the 59th second);
  • a: represents morning or afternoon, lowercase "am" or "pm";
  • A: represents morning or afternoon, capital "AM" or "PM";
  • w: represents the day of the week, represented by numbers 0 to 6 (0 represents Sunday, 1 represents Monday, and so on).

In addition, there are some other format parameters, which are not listed here. For more format parameters, you can view the detailed description of the date() function in PHP's official documentation.

In daily development, converting timestamps into readable time formats is a very common operation. By mastering the above content, I believe readers will be able to skillfully use the date() function to convert timestamps.

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