Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
执行操作时在PHP中计算时差时出现问题_Php_Time_Datediff - Fatal编程技术网

执行操作时在PHP中计算时差时出现问题

执行操作时在PHP中计算时差时出现问题,php,time,datediff,Php,Time,Datediff,我只是想知道我是否做得对。似乎动作是在没有时间检查的情况下完成的。你的父母不匹配 $timeFromMysql = strtotime($createdtime); $currenTime = SPRequest::now();; if ($diff = abs( strtotime( $timeFromMysql ) - strtotime( $currenTime ) > 30*24*60*60) { ACTION } 要进一步细分: if ($diff = abs( s

我只是想知道我是否做得对。似乎动作是在没有时间检查的情况下完成的。

你的父母不匹配

$timeFromMysql = strtotime($createdtime);
$currenTime = SPRequest::now();;

if ($diff = abs( strtotime( $timeFromMysql ) - strtotime( $currenTime )  > 30*24*60*60) {  
  ACTION
}
要进一步细分:

if ($diff = abs( strtotime( $timeFromMysql ) - strtotime( $currenTime ) )  > 30*24*60*60) {  
 //ACTION
}

计算圆括号并确保它们匹配。

不检查是否等于-将值赋给$diff,该值始终为真。因此:

if (
  $diff = abs(
    strtotime( $timeFromMysql ) - strtotime( $currenTime )
  )  > 30*24*60*60)
{  
 //ACTION
}

我得到以下错误:

PHP注意:在规则128中,类DateInterval的对象无法转换为int blabla

这是代码(128):

欲了解更多信息,请参阅更大的代码:

if ( $difference > 30*24*60*60) { 

echos工作正常。

是不是
CurrentTime
打字错误?@JoeFrambach这两个地方都一样;)不,我想OP希望将时间差存储在变量
$diff
中,然后对照
30*24*60*60
检查它。是的,这就是我想要的。数据库中有一个日期(&createdtime)。这是api将信息设置到数据库中的时间。一个月后,我想尽快更新。因此,应该检查当前时间和数据库时间之间的差异。但我在哪里遗漏了一些代码?
if ( $difference > 30*24*60*60) { 
$timeFromMysql = strtotime($createdtime);
$currenTime = SPRequest::now();

function format_interval(DateInterval $interval) {
    $result = "";
    if ($interval->y) { $result .= $interval->format("%y years "); }
    if ($interval->m) { $result .= $interval->format("%m months "); }
    if ($interval->d) { $result .= $interval->format("%d days "); }
    if ($interval->h) { $result .= $interval->format("%h hours "); }
    if ($interval->i) { $result .= $interval->format("%i minutes "); }
    if ($interval->s) { $result .= $interval->format("%s seconds "); }
    return $result;
}

$first_date = new DateTime($createdtime);
$second_date = new DateTime($currenTime);

$difference = $first_date->diff($second_date);

//echo format_interval($difference);
//echo $createdtime;
//echo $currenTime;

if ( $difference > 30*24*60*60) { 
    // load the apicall
    $xml = simplexml_load_file($api);
}