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

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

I just wondering if I am doing this right. it seems the action is done wihout the time check.

Kara
  • 6,115
  • 16
  • 50
  • 57
Simpel
  • 103
  • 1
  • 1
  • 7

3 Answers3

1

Your parens don't match.

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

To break it down further:

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

Count your parentheses and make sure they're matched.

000
  • 26,951
  • 10
  • 71
  • 101
0

You don't check if equals - you assign the value to $diff, which is always true. So:

$diff ==
djot
  • 2,952
  • 4
  • 19
  • 28
  • No I think OP wants to store the time diff in the variable `$diff`, then check it against `30*24*60*60`. – 000 Apr 14 '13 at 15:00
  • yes that's what I want. In the database there is a date (&createdtime). This was the time the api set info into the database. After a month I want to update no sooner. So the difference between the current and the database time should be checked. But where did I miss some code? – Simpel Apr 24 '13 at 17:19
0

I get the following error:

PHP Notice: Object of class DateInterval could not be converted to int blabla in rule 128

this is the code (128):

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

for more information here is a bigger code:

$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);
}

echos works fine.

Gwenc37
  • 2,064
  • 7
  • 18
  • 22
Simpel
  • 103
  • 1
  • 1
  • 7