当前位置:网站首页>PHP时间戳
PHP时间戳
2022-06-23 21:49:00 【游戏编程】
PHP 获取当前时间、时间戳及相互转换函数用法
1.获取当前日期字符串:date("Y-m-d H:i:s"); // 2017-12-14 23:13:512.获取当前时间戳:time(); // 15132642583.获取某个时间戳对应的日期字符串:date("Y-m-d H:i:s",1513264258); // 2017-12-14 23:13:514.获取某个日期的时间戳strtotime('2017-12-14 23:13:51'); // 15132642585.获取的时间与当前时间相差6小时解决方法这是因为时区设置问题,只要将之设为上海时间即可。方法如下:1.在php.ini中找到date.timezone,将它的值改成 Asia/Shanghai,即 date.timezone = Asia/Shanghai2.在程序开始时添加 date_default_timezone_set(‘Asia/Shanghai’)即可。date()、time()、strtotime() 函数的具体使用方法
语法: date()函数date(format,timestamp);定义和用法date()函数带有两个参数,第一个是格式字符串,第二个(可选)是UNIX时间戳。如果没有指定时间戳,在默认的情况下,date()函数将返回当前的日期和时间。有则返回时间戳对应的格式化日期字符串。例子:
<?php header("Content-type: text/html; charset=utf-8"); //将时区设置为上海时间 date_default_timezone_set('Asia/Shanghai'); echo date("Y-m-d H:i:s");语法: time()函数
time();定义和用法time() 函数返回自 Unix 纪元(January 1 1970 00:00:00 GMT)起的当前时间的秒数(时间戳)。语法:strtotime() 函数
strtotime(time,now);定义和用法strtotime() 函数将任何英文文本的日期或时间描述解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数)。 注意:如果年份表示使用两位数格式,则值 0-69 会映射为 2000-2069,值 70-100 会映射为 1970-2000。
注意:请注意 m/d/y 或 d-m-y 格式的日期,如果分隔符是斜线(/),则使用美洲的 m/d/y 格式。如果分隔符是横杠(-)或者点(.),则使用欧洲的 d-m-y 格式。为了避免潜在的错误,您应该尽可能使用 YYYY-MM-DD 格式或者使用 date_create_from_format() 函数。
例子:
<?php header("Content-type: text/html; charset=utf-8"); date_default_timezone_set('Asia/Shanghai'); echo(strtotime("now") . "<br>"); echo(strtotime("15 October 2019") . "<br>"); echo(strtotime("+5 hours") . "<br>"); echo(strtotime("+1 week") . "<br>"); echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>"); echo(strtotime("next Monday") . "<br>"); echo(strtotime("last Sunday")); echo(strtotime("2011-11-11 11:11:11")); 如下字符表示date()函数的规定输出日期字符串的格式:
d - 一个月中的第几天(从 01 到 31)
D - 星期几的文本表示(用三个字母表示)
j - 一个月中的第几天,不带前导零(1 到 31)
l(’L’ 的小写形式)- 星期几的完整的文本表示
N - 星期几的 ISO-8601 数字格式表示(1表示Monday[星期一],7表示Sunday[星期日])
S - 一个月中的第几天的英语序数后缀(2 个字符:st、nd、rd 或 th。与 j 搭配使用)
w - 星期几的数字表示(0 表示 Sunday[星期日],6 表示 Saturday[星期六])
z - 一年中的第几天(从 0 到 365)
W - 用 ISO-8601 数字格式表示一年中的星期数字(每周从 Monday[星期一]开始)
F - 月份的完整的文本表示(January[一月份] 到 December[十二月份])
m - 月份的数字表示(从 01 到 12)
M - 月份的短文本表示(用三个字母表示)
n - 月份的数字表示,不带前导零(1 到 12)
t - 给定月份中包含的天数
L - 是否是闰年(如果是闰年则为 1,否则为 0)
o - ISO-8601 标准下的年份数字
Y - 年份的四位数表示
y - 年份的两位数表示
a - 小写形式表示:am 或 pm
A - 大写形式表示:AM 或 PM
B - Swatch Internet Time(000 到 999)
g - 12 小时制,不带前导零(1 到 12)
G - 24 小时制,不带前导零(0 到 23)
h - 12 小时制,带前导零(01 到 12)
H - 24 小时制,带前导零(00 到 23)
i - 分,带前导零(00 到 59)
s - 秒,带前导零(00 到 59)
u - 微秒(PHP 5.2.2 中新增的)
e - 时区标识符(例如:UTC、GMT、Atlantic/Azores)
I(i 的大写形式)- 日期是否是在夏令时(如果是夏令时则为 1,否则为 0)
O - 格林威治时间(GMT)的差值,单位是小时(实例:+0100)
P - 格林威治时间(GMT)的差值,单位是 hours:minutes(PHP 5.1.3 中新增的)
T - 时区的简写(实例:EST、MDT)
Z - 以秒为单位的时区偏移量。UTC 以西时区的偏移量为负数(-43200 到 50400)
c - ISO-8601 标准的日期(例如 2013-05-05T16:34:42+00:00)
r - RFC 2822 格式的日期(例如 Fri, 12 Apr 2013 12:01:05 +0200)
U - 自 Unix 纪元(January 1 1970 00:00:00 GMT)以来经过的秒数
同时,也可使用下列预定义常量(从 PHP 5.1.0 开始可用):
DATE_ATOM - Atom(例如:2013-04-12T15:52:01+00:00)
DATE_COOKIE - HTTP Cookies(例如:Friday, 12-Apr-13 15:52:01 UTC)
DATE_ISO8601 - ISO-8601(例如:2013-04-12T15:52:01+0000)
DATE_RFC822 - RFC 822(例如:Fri, 12 Apr 13 15:52:01 +0000)
DATE_RFC850 - RFC 850(例如:Friday, 12-Apr-13 15:52:01 UTC)
DATE_RFC1036 - RFC 1036(例如:Fri, 12 Apr 13 15:52:01 +0000)
DATE_RFC1123 - RFC 1123(例如:Fri, 12 Apr 2013 15:52:01 +0000)
DATE_RFC2822 - RFC 2822(Fri, 12 Apr 2013 15:52:01 +0000)
DATE_RFC3339 - 与 DATE_ATOM 相同(从 PHP 5.1.3 开始)
DATE_RSS - RSS(Fri, 12 Aug 2013 15:52:01 +0000)
DATE_W3C - 万维网联盟(例如:2013-04-12T15:52:01+00:00)
作者:Asteroid 325
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
- Why is only one value displayed on your data graph?
- How to deploy API gateways and split services under multi services?
- What are the operation and maintenance advantages of Fortress machine web application publishing server? Two outstanding advantages
- Ranking of high cost performance commercial endowment insurance products in 2022
- Why is the server fortress machine error code 110? How to solve error code 110?
- Go build command (go language compilation command) complete introduction
- How to set secondary title in website construction what is the function of secondary title
- Three ways to enable IPv6 on Tencent cloud
- Talk about the problems and solutions of IT enterprise fixed assets management system
- Slsa: accelerator for successful SBOM
猜你喜欢

应用实践 | Apache Doris 整合 Iceberg + Flink CDC 构建实时湖仓一体的联邦查询分析架构

蚂蚁集团自研TEE技术通过国家级金融科技产品认证

Hackinglab penetration test question 8:key can't find it again

Slsa: accelerator for successful SBOM
![[technical dry goods] the technical construction route and characteristics of zero trust in ant Office](/img/d1/ce999b9f72bbb8f692c4298b4042aa.png)
[technical dry goods] the technical construction route and characteristics of zero trust in ant Office

Opengauss Developer Day 2022 was officially launched to build an open source database root community with developers

脚本之美│VBS 入门交互实战

SLSA: 成功SBOM的促进剂

Beauty of script │ VBS introduction interactive practice

游戏安全丨喊话CALL分析-写代码
随机推荐
How to use xshell to log in to the server through the fortress machine? How does the fortress machine configure the tunnel?
[tcapulusdb knowledge base] example of deleting data (TDR table)
Flutter Utils
蚂蚁集团自研TEE技术通过国家级金融科技产品认证
The old CVM of Tencent cloud is migrated to the new CVM, and the IP remains unchanged
游戏安全丨喊话CALL分析-写代码
Understand the data consistency between MySQL and redis
Talking about using email to attack social engineering
How to set the search bar of website construction and what should be paid attention to when designing the search box
Chaos engineering, learn about it
Intelligent storage | high speed HD media processing capability
What are the application flow restrictions of API gateway framework?
Impala port
Go build command (go language compilation command) complete introduction
How to build a business analysis system
How to set the text style as PG in website construction
Opengauss Developer Day 2022 was officially launched to build an open source database root community with developers
Micro API gateway Middleware
How to use fortress remote server two types of Fortress
How to set the website address for website construction can the website be put on record