php – strtotime返回false

我的日期字符串看起来像这样:
"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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...