0

PHP docs claims that DateTime::modify() and strtotime() both should return false in case of error, but I'm getting this:

> 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

If wesdek and weeekc are valid strings, what are they meaning?

P.S. I've seen similar SO question, but here I've shown that obvious methods from there aren't always working…

Storm
  • 360
  • 4
  • 16

1 Answers1

0

While writing this, I thought about something like

function isModifyArgumentValidRelative (string $modifyString)
{
    $now = new DateTime();
    $notNow = clone $now;
    @$modifyResult = $notNow->modify("-{$modifyString}");
    if (false === $modifyResult || $notNow == $now) {
        return false;
    }
    return true;
}

Seems like it works, except 0 seconds or 0 minutes and so on, of course :)

Storm
  • 360
  • 4
  • 16