加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – strtotime返回false

发布时间:2020-12-13 18:26:59 所属栏目:PHP教程 来源:网络整理
导读:我的日期字符串看起来像这样: "2012 28 Nov 21:00 CET" strtotime(“2012年11月28日11:00 21:00 CET”)返回false. 有没有办法将此字符串转换为日期对象,还是需要以不同方式对其进行格式化? 2012年11月28日21:00 CET是一种奇怪的日期/时间格式. Y型d-M?你
我的日期字符串看起来像这样:
"2012 28 Nov 21:00 CET"

strtotime(“2012年11月28日11:00 21:00 CET”)返回false.

有没有办法将此字符串转换为日期对象,还是需要以不同方式对其进行格式化?

2012年11月28日21:00 CET是一种奇怪的日期/时间格式. Y型d-M?你在哪里得到的?

无论如何,DateTime对象有一个方法createFromFormat,它可以更好地解析:

$dt = DateTime::createFromFormat("Y d M H:i T",'2012 28 Nov 21:00 CET');
$ts = $dt->getTimestamp();
echo $ts; // 1354132800

在这里试试:http://codepad.viper-7.com/NfAmcw

strtotime期望“英文文本日期时间”(according to the manual),Y-D-M不是.任何时候strtotime返回false,它只是不理解你的字符串,在这个应用程序中是预期的.手册页上的注释处理此问题:

Note:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at
the separator between the various components: if the separator is a
slash (/),then the American m/d/y is assumed; whereas if the
separator is a dash (-) or a dot (.),then the European d-m-y format
is assumed.

To avoid potential ambiguity,it’s best to use ISO 8601 (YYYY-MM-DD)
dates or DateTime::createFromFormat() when possible.

同样,DateTime是任何与日期或时间交互的优秀工具.

文档

> DateTime类 – http://www.php.net/manual/en/class.datetime.php
> DateTime :: createFromFormat – http://www.php.net/manual/en/datetime.createfromformat.php
> strtotime – http://php.net/manual/en/function.strtotime.php
> date(还列出日期格式字符串) – http://php.net/manual/en/function.date.php

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读