Php 弦乐;int在strotime中工作不正常
在这种情况下,Php 弦乐;int在strotime中工作不正常,php,strtotime,Php,Strtotime,在这种情况下,$interval我以整数形式给出表示返回true时工作正常,假设$interval我以字符串形式给出表示返回false时工作不正常 情景1 情景2 我的预期出局了 场景2它也应该返回为array(1){[“passwordResetStatus”]=>bool(true)}显然,您不能将数学方程放入字符串中,但如果出于某种原因,您得到的是字符串格式,并且您确信它们将始终是这样的格式,您可以解析它 f、 e: $interval = array_product(explode('*
$interval
我以整数形式给出表示返回true时工作正常,假设$interval
我以字符串形式给出表示返回false时工作不正常
情景1
情景2
我的预期出局了
场景2它也应该返回为
array(1){[“passwordResetStatus”]=>bool(true)}
显然,您不能将数学方程放入字符串中,但如果出于某种原因,您得到的是字符串格式,并且您确信它们将始终是这样的格式,您可以解析它
f、 e:
$interval = array_product(explode('*',"60 * 60 * 24 * 7"));
很明显,你不能把数学方程放在字符串中,但如果出于某种原因,你把它们放在字符串格式中,并且你确信它们总是这样的格式,你可以解析它 f、 e:
$interval = array_product(explode('*',"60 * 60 * 24 * 7"));
用户不能输入“1周”而不是“60*60*24*7”吗 如果是,比eval更安全的方法是使用strotime来计算时间
echo strtotime("1 week")-time();
// Same as 60*60*24*7
用户不能输入“1周”而不是“60*60*24*7”吗 如果是,比eval更安全的方法是使用strotime来计算时间
echo strtotime("1 week")-time();
// Same as 60*60*24*7
为什么不使用PHP DateTime类?这里有一个简短的解决方案
$today = new \DateTime();
$rest = new \DateTime('2018-11-21 11:58:55');
$interval = $rest->diff($today);
$passwordValid = $interval->format('%a') >= 7 ? false : true;
我在这里做了什么?首先我们需要今天的时间。在那之后,我们需要时间,与今天的时间相比。这两个时间都是日期时间实例。因此,我们可以很容易地计算两者之间的时间差。DateTime类获得了diff
方法,该方法计算两个DateTime对象之间的差异。它返回一个保存差异的DateInterval
对象。现在,我们可以将计算出的差异与您的7天间隔进行比较
很简单,嗯?为什么不使用PHP DateTime类呢?这里有一个简短的解决方案
$today = new \DateTime();
$rest = new \DateTime('2018-11-21 11:58:55');
$interval = $rest->diff($today);
$passwordValid = $interval->format('%a') >= 7 ? false : true;
我在这里做了什么?首先我们需要今天的时间。在那之后,我们需要时间,与今天的时间相比。这两个时间都是日期时间实例。因此,我们可以很容易地计算两者之间的时间差。DateTime类获得了diff
方法,该方法计算两个DateTime对象之间的差异。它返回一个保存差异的DateInterval
对象。现在,我们可以将计算出的差异与您的7天间隔进行比较
很简单,嗯?这对用户来说更直观,指向可能值的链接会更有用。也许这对用户来说更直观,指向可能值的链接会更有用。也许更多
$today = new \DateTime();
$rest = new \DateTime('2018-11-21 11:58:55');
$interval = $rest->diff($today);
$passwordValid = $interval->format('%a') >= 7 ? false : true;