Php 是否有可靠的方法验证相对格式的DateTime::modify()参数
声明如果出现错误,Php 是否有可靠的方法验证相对格式的DateTime::modify()参数,php,datetime,Php,Datetime,声明如果出现错误,DateTime::modify()和strotime()都应该返回false,但我得到了以下信息: > php -r 'var_dump(time(), strtotime("-1 week"), strtotime("-1 wesdek"), (new DateTime())->modify("-1 weeekc"));' int(1533556632) int(1532951832) int(1533560232) object(DateTime)#1 (3)
DateTime::modify()
和strotime()
都应该返回false
,但我得到了以下信息:
> php -r 'var_dump(time(), strtotime("-1 week"), strtotime("-1 wesdek"), (new DateTime())->modify("-1 weeekc"));'
int(1533556632)
int(1532951832)
int(1533560232)
object(DateTime)#1 (3) {
["date"]=>
string(26) "2018-08-06 11:57:12.797259"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
> php -v
PHP 7.2.7 (cli) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
如果wesdek
和weeekc
是有效字符串,它们是什么意思
顺便说一下,我已经看到了,但这里我已经展示了,来自那里的明显方法并不总是有效的…在写这篇文章的时候,我想到了类似的事情
function isModifyArgumentValidRelative (string $modifyString)
{
$now = new DateTime();
$notNow = clone $now;
@$modifyResult = $notNow->modify("-{$modifyString}");
if (false === $modifyResult || $notNow == $now) {
return false;
}
return true;
}
它似乎可以工作,除了0秒
或0分钟
等等,当然:)