1

Question: How the best to convert $p=' "'.implode('","', $person).'"'; $p to an integer?

What I have: I am trying to use an if statement telling $person == false;

$x['date']; is the timestamp in my database. I have worked out the time difference, now I am trying to make the person disappear if post over 3 seconds. so I used $t > 3 seconds then the $p == false; The difficulty for my was the $t was implode so it a single string. I was trying to use preg_match, but I don't think this is a good idea. I am trying to use $difference = settype($t, "integer"); but I am getting a boolean rather than a number.

$diff = array();
$person = array();
foreach($stmt as $x)
{  
 $person[] =   $x['names']. $x['ages'];

$posts[] = $x['date'];
$timePosted = new DateTime($posts[] = $x['date']);
echo 'Time Posted : '. $timePosted ->format("d-m-Y H:i:s");
echo "</br>";

date_default_timezone_set('Europe/London');
$today = date('d-m-Y H:i:s');
echo 'Current time is : '. $today;
echo "</br>"; 

$today = new DateTime(date('d-m-Y H:i:s'));
$interval = $timePosted->diff($today);
"Difference " . $interval->d. " days ". $interval->h. " hours ".$interval->i." minutes ".$interval->s." seconds ";
echo "</br>";

$diff[] = $interval->h. " hours ".$interval->i." minutes ".$interval->s." seconds ";
$diff[] = $interval->s;  //last array seconds
}
$p=' "'.implode('","', $person).'"';      
echo $t= ' "'.implode('","', $diff).'"'."<br />";
$difference = settype($t, "integer");
echo gettype($difference);
echo "</br>";



if( $t >3){
  $p == false;
}else{
 echo "its okay, smaller than 3 seconds";
}
4
  • 2
    its a very long question, and not easy to grasp what you are saying at first, but I saw you asking "convert $p to an integer?", maybe $p = intval($p), converts it to a proper integer? IF not, can you make your question shorter and concise. Commented Mar 20, 2016 at 18:17
  • And what is the value of $t? Commented Mar 20, 2016 at 18:18
  • @MuhammedM. yes, you are right. sorry about the question, I was trying to explain what I have before asking question, I will try to edit the question. thanks Commented Mar 20, 2016 at 18:22
  • @u_mulder t is all user posts time difference(seconds) from the time they post and now. Commented Mar 20, 2016 at 18:25

1 Answer 1

1

The problem is you're setting $difference = settype($t, "integer");

The settype function returns a boolean. The value $t should be set to an integer, so to test, use echo gettype($t); instead of echo gettype($difference);

also, you're using a comparison operator instead of the assignment in

if( $t >3){
  $p == false;

it should be

if( $t >3){
  $p = false;
Sign up to request clarification or add additional context in comments.

1 Comment

he is actually right, if you are doing settype do this settype($t, "integer"); instead of this: $difference = settype($t, "integer");

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.