How to convert timestamp to time format in php?
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)".
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])
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:
format character | Description | Return value example |
---|---|---|
日 | --- | --- |
d | The day of the month is represented by two digits. If there are less than two digits, add 0 | 01 to 31 |
The English abbreviation of the day of the week (using 3 letters) | Mon to Sun | |
j | in the month Days without leading zeros1 to 31 | |
English words for days of the week | Sunday 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 | |
w | Use numbers to represent the day of the week | 0 (for Sunday) to 6 (for Saturday) |
z | Use numbers to represent the days of the year | 0 to 365 |
Week | --- | --- |
According 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) | |
月 | ------ | |
Month in English Words, such as January or June | January to December | |
Use two digits to represent the current month | 01 to 12 | |
The English abbreviation of the month | Jan to Dec | |
Use numbers to represent the current month | 1 to 12 | |
Specify the number of days in the month | 28 to 31 | |
--- | --- | |
Whether the specified year is a leap year | If 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 | ##y |
For example: 99 or 03 | ||
--- | ---a | |
am or pm | ##A | |
AM or PM | B | |
000 to 999 | g | |
1 to 12 | G | |
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 |
完整的日期/时间 | --- | --- |
c | ISO-8601 格式的日期 | 2014-02-12T15:19:21+00:00 |
r | RFC 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('l dS \\\\\\\\\\\\\\\\of F Y h:i:s A',"1220976000")."<br>"; echo date("Y/m/d H:i:s","1604629565")."<br>"; echo date("Y.m.d H:i:s","1604545871"); ?>
输出:
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
示例2:
<?php header('content-type:text/html;charset=utf-8'); // 设定要用的时区 date_default_timezone_set('PRC'); // 输出类似Monday echo date("l"); echo "<br/>"; // 输出类似Monday 15th of August 2005 03:12:46 PM echo date('l dS \\\\\\\\\\\\\\\\of F Y h:i:s A'); 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('Y-m-d H:i:s').'<br>'; $time = time(); echo '是一周中的第 '.date('w', $time).' 天<br>'; echo '今年是:'; date('L', $time)? print('闰年'):print('平年'); ?>
输出:
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 天 今年是:闰年
更多编程相关知识,请访问:编程教学!!
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
