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

How to convert timestamp to time format in php?

Nov 05, 2020 am 10:26 AM
php Timestamp

In PHP, you can use the date() function to convert the timestamp into a time format. This function can format the timestamp into a more readable date and time. The syntax format is "date(" time The output format of the stamp is ",timestamp)", for example "date("Y-m-d H:i:s",timestamp)".

How to convert timestamp to time format in php?

Recommended: "PHP Video Tutorial"

When the time and date are saved in the computer, you can use UNIX timestamp as standard format. However, the readability of UNIX timestamps is very poor, so sometimes we need to format the UNIX timestamps into more readable time and date, or format them into the format required by other software.

In PHP, you can use the date() function to format the timestamp into a more readable date and time.

The syntax format of this function is as follows:

date($format [,$timestamp])
Copy after login

Parameters:

  • format is required. Specifies the format of the timestamp.

  • timestamp Optional. Specify timestamp. The default is the current date and time.

Parameter $format The special characters that can be recognized in the format string are as shown in the following table:

##DThe English abbreviation of the day of the week (using 3 letters)Mon to Sun in the month Days without leading zeros ##l (lowercase letter of "L")NS##WAccording to ISO-8601 standard format , using numbers to represent the week of the year, each week starting from Monday, (new in PHP 4.1.0) For example: 42 (the 42nd week of the year)------F Month in English Words, such as January or JuneJanuary to DecembermUse two digits to represent the current month01 to 12M The English abbreviation of the month Jan to DecnUse numbers to represent the current month1 to 12tSpecify the number of days in the month28 to 31##年LoY##yUse 2 digits to represent the yearFor example: 99 or 03Time---aLowercase AM and PM valuesam or pmUppercase AM and PM valuesSwatch Internet standard time Use 12-hour format to represent hoursUse 24-hour format to represent hours
format characterDescriptionReturn value example
------
d The day of the month is represented by two digits. If there are less than two digits, add 001 to 31
j1 to 31
English words for days of the weekSunday to Saturday
According to the ISO-8601 standard format, using numbers to represent the middle of the week The number of days (newly added in PHP5.1.0) 1 (meaning Monday) to 7 (meaning Sunday)
Every month The English suffix after the number of days (represented by 2 characters) st, nd, rd or th.Can be used with j
wUse numbers to represent the day of the week0 (for Sunday) to 6 (for Saturday)
zUse numbers to represent the days of the year0 to 365
Week------
------
Whether the specified year is a leap yearIf it is a leap year, the value is 1, otherwise it is 0
According to the ISO-8601 standard The format uses numbers to represent the year, which has the same effect as Y (new in PHP 5.1.0) 1999 or 2019
Use 4 One digit represents the complete year For example: 1999 or 2019
---
##A
AM or PMB
000 to 999g
1 to 12G
0 to 23
h使用 12 小时格式表示小时数,有前导零01 到 12
H使用 24 小时格式表示小时数,有前导零00 到 23
i使用两位数字表示分钟数,有前导零00 到 59>
s使用两位数字表示秒数,有前导零00 到 59>
u毫秒(PHP 5.2.2 新增)。需要注意的是 date() 函数总是返回 000000,因为它只接受 integer 参数,而 DateTime::format() 才支持毫秒例如:654321
时区------
e时区标识例如:UTC、GMT、Atlantic/Azores
I(大写的“i”)是否为夏令时夏令时为 1,否则为 0
O与格林尼治时间相差的小时数例如:+0200
P与格林尼治时间(GMT)的差别,小时和分钟之间由冒号分隔例如:+02:00
T本机所在的时区例如:EST、MDT(在 windows 下为完整文本格式,例如“Eastern Standard Time”,中文版会显示“中国标准时间”)
Z时差偏移量的秒数,UTC 西边的时区偏移量总是负的,UTC 东边的的时区偏移量总是正的-43200 到 43200
完整的日期/时间------
cISO-8601 格式的日期2014-02-12T15:19:21+00:00
rRFC 822 格式的日期例如:Thu,21 Dec 2000 16:01:07 +0200
U从 UNIX 纪元(January 1 1970 00:00:00 GMT)开始至今的秒数和 time() 返回相同的时间戳

提示:特殊字符中不能被识别的字符将原样显示,Z 字符在使用 gmdate() 函数时总是返回 0。

示例1:

<?php
echo date("Y-m-d H:i:s","1256112010")."<br>";
echo date(&#39;l dS \\\\\\\\\\\\\\\\of F Y h:i:s A&#39;,"1220976000")."<br>";
echo date("Y/m/d H:i:s","1604629565")."<br>";
echo date("Y.m.d H:i:s","1604545871");
?>
Copy after login

输出:

2009-10-21 16:00:10
Wednesday 10th of September 2008 12:00:00 AM
2020/11/06 10:26:05
2020.11.05 11:11:11
Copy after login

示例2:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
// 设定要用的时区
date_default_timezone_set(&#39;PRC&#39;);
// 输出类似Monday
echo date("l");
echo "<br/>";
// 输出类似Monday 15th of August 2005 03:12:46 PM
echo date(&#39;l dS \\\\\\\\\\\\\\\\of F Y h:i:s A&#39;);
echo "<br/>";
// 输出July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l");
echo "<br/>";
/* 在格式参数中使用常量 */
// 输出类似Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822);
echo "<br/>";
// 输出类似2000-07-01T00:00:00+00:00
echo date(DATE_ATOM);
echo "<br/>";
//输出类似2000-07-01 14:00:00
echo date(&#39;Y-m-d H:i:s&#39;).&#39;<br>&#39;;
$time = time();
echo &#39;是一周中的第 &#39;.date(&#39;w&#39;, $time).&#39; 天<br>&#39;;
echo &#39;今年是:&#39;;
date(&#39;L&#39;, $time)? print(&#39;闰年&#39;):print(&#39;平年&#39;);
?>
Copy after login

输出:

Thursday
Thursday 05th of November 2020 10:32:44 AM
July 1, 2000 is on a Thursday
Thu, 05 Nov 2020 10:32:44 +0800
2020-11-05T10:32:44+08:00
2020-11-05 10:32:44
是一周中的第 4 天
今年是:闰年
Copy after login

更多编程相关知识,请访问:编程教学!!

The above is the detailed content of How to convert timestamp to time format in php?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

See all articles