DateTime::createFromFormat - php 格式问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3467315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DateTime::createFromFormat - php format issue
提问by roger rover
PHP - DateTime::createFromFormat — Returns new DateTime object formatted according to the specified format
PHP - DateTime::createFromFormat — 返回按照指定格式格式化的新 DateTime 对象
this works:
这有效:
$var = DateTime::createFromFormat('Ymd','20100809')->getTimestamp();
but this fails with
但这失败了
Call to a member function getTimestamp() on a non-object
在非对象上调用成员函数 getTimestamp()
$var = DateTime::createFromFormat('Y/m/d H:M:S','2010/08/09 07:47:00')->getTimestamp();
回答by Pekka
In the case at hand, the M:S
portion is wrong. It needs to be i:s
. See the manual on date().
在手头的情况下,该M:S
部分是错误的。它需要是i:s
。请参阅date() 上的手册。
However, this highlights a deeper conceptual problem: An incorrect input in either parameter will lead to a fatal error, which is bad behaviour for an application in production.
然而,这凸显了一个更深层次的概念问题:任何一个参数中的错误输入都会导致致命错误,这对于生产中的应用程序来说是一种不良行为。
From the manual on createFromFormat
:
从手册上createFromFormat
:
Returns a new DateTime instance or FALSE on failure.
失败时返回一个新的 DateTime 实例或 FALSE。
When the call fails to build a date from your input, no object is returned.
当调用无法根据您的输入构建日期时,不会返回任何对象。
To avoid fatal errors on incorrect inputs, you would (sadly, as this breaks the nice chaining) have to check for success first:
为了避免错误输入的致命错误,您将(可悲的是,因为这破坏了良好的链接)必须首先检查是否成功:
$var = DateTime::createFromFormat('Y/m/d H:M:S','2010/08/09 07:47:00');
if ($var instanceof DateTime)
echo $var->getTimestamp();
回答by Artefacto
It should be
它应该是
DateTime::createFromFormat('Y/m/d H:i:s','2010/08/09 07:47:00')->getTimestamp()
^ ^
See date
for the format used.
请参阅date
使用的格式。
You could also use strtotime
in this circumstance. This would give the same result:
您也可以strtotime
在这种情况下使用。这将给出相同的结果:
strtotime('2010/08/09 07:47:00')
Another way:
其它的办法:
date_create('2010/08/09 07:47:00')->getTimestamp()
Note that DateTime::createFromFormat
returns FALSE
on error. You can fetch the errors with DateTime::getLastErrors()
:
请注意,错误DateTime::createFromFormat
返回FALSE
。您可以使用以下方法获取错误DateTime::getLastErrors()
:
<?php
$d = DateTime::createFromFormat('Y/m/d H:M:S','2010/08/09 07:47:00');
var_dump($d);
var_dump(DateTime::getLastErrors());
would give:
会给:
bool(false) array(4) { ["warning_count"]=> int(0) ["warnings"]=> array(0) { } ["error_count"]=> int(3) ["errors"]=> array(1) { [14]=> string(13) "Trailing data" } }
回答by codaddict
Use sin place of Sand min place of M
使用s代替S和m代替M
- s - Seconds, with leading zeros
- S - English ordinal suffix for the day of the month, 2 characters like st, nd, rd or th
- s - 秒,带前导零
- S - 月份日期的英文序数后缀,2 个字符,如 st、nd、rd 或 th
and
和
- m - Numeric representation of a
month, with leading zeros - M - short textual representation of a month, three letters like Jan through Dec
- m -
月份的数字表示,带前导零 - M - 一个月的简短文本表示,三个字母,如 Jan 到 Dec
回答by Mark Baker
H:M:S M is month short name and S is ordinal (st, nd,rd,th) day of month try H:i:s
H:M:S M 是月份的简称,S 是月份的序数 (st, nd,rd,th) 尝试 H:i:s