PHP docs声称DateTime::modify()
和strtotime()
都应在发生错误的情况下返回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
是有效的字符串,它们的含义是什么?
P.S。我见过similar SO question,但在这里我证明了显而易见的方法并不总是有效的……
答案 0 :(得分:0)
在撰写本文时,我想到了类似的东西
function isModifyArgumentValidRelative (string $modifyString)
{
$now = new DateTime();
$notNow = clone $now;
@$modifyResult = $notNow->modify("-{$modifyString}");
if (false === $modifyResult || $notNow == $now) {
return false;
}
return true;
}
看起来似乎可行,除了0 seconds
或0 minutes
等,当然:)