Jump to content

Time problem


Go to solution Solved by Barand,

Recommended Posts

Hi I'm currently having a problem with my time ban option.

 

Part 1 inserting into database if banned for 1 hour

if($_GET['ban']=='1') {
$ip = $_SERVER["HTTP_X_MXIT_USERID_R"];
if(!isset($ip))
{
	$ip = "Debater";
}



$result = mysql_query("SELECT * FROM Users2 WHERE mxitid = \"$ip\"");
        $row = mysql_fetch_array($result);
         $name = $row['Username'];




$post_time = date("U");

$mxitid = $_GET["ip"];
$user = $_GET["nname"];
$expire = time() + (3600000 * 1);
$banby = $name; 
$banby2 = $_SERVER["HTTP_X_MXIT_USERID_R"];

$IP_To_Remove = $myrow["ip"];
      $query = "UPDATE timeban SET user='$user', bantime='$expire', banby='$banby'  WHERE mxitid='".$mxitid."'"; 
      mysql_query($query);

Part 2: checking if banned

    $ip = $_SERVER["HTTP_X_MXIT_USERID_R"];

if(!isset($ip))
{
	$ip = "Debater";
}
$timet = time();

    $sql = "SELECT * FROM StringyChat_IPBan WHERE ip=\"$ip\"";
    $result = mysql_query($sql);
    $myrow = mysql_fetch_array($result);

    $sqlt = "SELECT * FROM timeban WHERE mxitid=\"$ip\"";
    $resultt = mysql_query($sqlt);
    $myrowt = mysql_fetch_array($resultt);

if($resultt === FALSE) { 
    die(mysql_error()); // TODO: better error handling
}
elseif($timet > $myrowt["bantime"]){
?>

<form name="StringyChat_form" method="POST" action="<? echo $_SERVER['REQUEST_URI']; ?>">
      <input type="hidden" name="StringyChat_name" class="StringyChatFrm" value="<?php echo $name ?>" size="20">
      <textarea name="StringyChat_message" class="StringyChatFrm" cols="20" rows="1"></textarea>
      <br>
      <input name="StringyChat_submit" class="StringyChatFrm" type="submit" value="Post Message">
    </form>

<?
}
elseif($timet < $myrowt["bantime"]){
$expire = $myrowt["bantime"];
$now = time();
echo "You will be unkicked in " . dateDiff($expire, time(), 6) . "\n";


    }

    elseif($myrow["ip"] == "") {	// Checks if IP not found in banned list




?>
<form name="StringyChat_form" method="POST" action="<? echo $_SERVER['REQUEST_URI']; ?>">
      <input type="hidden" name="StringyChat_name" class="StringyChatFrm" value="<?php echo $name ?>" size="20">
      <textarea name="StringyChat_message" class="StringyChatFrm" cols="20" rows="1"></textarea>
      <br>
      <input name="StringyChat_submit" class="StringyChatFrm" type="submit" value="Post Message">
    </form>

<?
}



 else {
      echo "<span style='color:#10ce59'><u>Dear User, you have been banned from the Chat due to not following the rules. You will need to come back regularly to see if you were unbanned. Until then, goodbye!</u></span><br>";
    }

Part 3 Datediff funtion

  function dateDiff($time1, $time2, $precision = 6) {
    // If not numeric then convert texts to unix timestamps
    if (!is_int($time1)) {
      $time1 = strtotime($time1);
    }
    if (!is_int($time2)) {
      $time2 = strtotime($time2);
    }

    // If time1 is bigger than time2
    // Then swap time1 and time2
    if ($time1 > $time2) {
      $ttime = $time1;
      $time1 = $time2;
      $time2 = $ttime;
    }

    // Set up intervals and diffs arrays
    $intervals = array('year','month','day','hour','minute','second');
    $diffs = array();

    // Loop thru all intervals
    foreach ($intervals as $interval) {
      // Create temp time from time1 and interval
      $ttime = strtotime('+1 ' . $interval, $time1);
      // Set initial values
      $add = 1;
      $looped = 0;
      // Loop until temp time is smaller than time2
      while ($time2 >= $ttime) {
        // Create new temp time from time1 and interval
        $add++;
        $ttime = strtotime("+" . $add . " " . $interval, $time1);
        $looped++;
      }
 
      $time1 = strtotime("+" . $looped . " " . $interval, $time1);
      $diffs[$interval] = $looped;
    }
    
    $count = 0;
    $times = array();
    // Loop thru all diffs
    foreach ($diffs as $interval => $value) {
      // Break if we have needed precission
      if ($count >= $precision) {
	break;
      }
      // Add value and interval 
      // if value is bigger than 0
      if ($value > 0) {
	// Add s if value is not 1
	if ($value != 1) {
	  $interval .= "s";
	}
	// Add value and interval to times array
	$times[] = $value . " " . $interval;
	$count++;
      }
    }

    // Return string with times
    return implode(", ", $times);
  }

The problem is I banned the user in part 1 for 1 hour but on the output of part 2 it shows up as 

You will be unkicked in 45 years, 2 months, 2 days, 13 hours, 13 minutes, 8 seconds 

 

 and it just keep increasing each time I refresh instead of decreasing

Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/
Share on other sites

1 hour = 3600 seconds.

3600000 seconds, which you are adding, = 41 days 16 hours

 

And there is an easier way of getting a date difference

$dt1 = new DateTime('2000-01-01');
$dt2=new DateTime(); // Now
$dif = $dt2->diff($dt1);
echo $dif->format('%y years, %m months, %d days, %h hours, %i minutes, %s seconds');

// RESULT---> 15 years, 2 months, 2 days, 15 hours, 15 minutes, 15 seconds
Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/#findComment-1507360
Share on other sites

 

1 hour = 3600 seconds.

3600000 seconds, which you are adding, = 41 days 16 hours

 

And there is an easier way of getting a date difference

$dt1 = new DateTime('2000-01-01');
$dt2=new DateTime(); // Now
$dif = $dt2->diff($dt1);
echo $dif->format('%y years, %m months, %d days, %h hours, %i minutes, %s seconds');

// RESULT---> 15 years, 2 months, 2 days, 15 hours, 15 minutes, 15 seconds

Ok I changed

$expire = time() + (3600000 * 1);

to

$expire = time() + (3600 * 1);

and

 

$expire = $myrowt["bantime"];
$now = time();
echo "You will be unkicked in " . dateDiff($expire, time(), 6) . "<br>";

to

elseif($timet < $myrowt["bantime"]){
$expire = $myrowt["bantime"];
$dt1 = new DateTime($expire);
$dt2=new DateTime(); // Now
$dif = $dt2->diff($dt1);
echo $dif->format('%y years, %m months, %d days, %h hours, %i minutes, %s seconds');

    }

but still it shows

 

1938 years, 0 months, 0 days, 3 hours, 20 minutes, 6 seconds

 

and the seconds keep increasing

Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/#findComment-1507362
Share on other sites

Sorry i did not read al of your code but it seems to be more complex then needed.

 

First about IP-banning.

If you use ip banning instead of user banning please do realise that you could ban a complete officebuilding of hospital ! Otherwise mobile devices are switching ip's many times. I always say: forget this whole IP-shit!

 

that said,

 

Did you ever try to use the DateTime and DateInterval standard PHP objects?

<?php

$last = new DateTime('2015-1-26 11:53:12');

$now = new DateTime();

$diff = $now->diff($last);

echo 'Difference is:<br>';

if($diff->y) echo $diff->y . ' years<br>';
if($diff->m) echo $diff->m . ' months<br>';
if($diff->d) echo $diff->d . ' days<br>';
if($diff->h) echo $diff->h . ' hours<br>';
if($diff->i) echo $diff->i . ' minutes<br>';
if($diff->s) echo $diff->s . ' seconds<br>';

?>
Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/#findComment-1507363
Share on other sites

 

Sorry i did not read al of your code but it seems to be more complex then needed.

 

First about IP-banning.

If you use ip banning instead of user banning please do realise that you could ban a complete officebuilding of hospital ! Otherwise mobile devices are switching ip's many times. I always say: forget this whole IP-shit!

 

that said,

 

Did you ever try to use the DateTime and DateInterval standard PHP objects?

<?php

$last = new DateTime('2015-1-26 11:53:12');

$now = new DateTime();

$diff = $now->diff($last);

echo 'Difference is:<br>';

if($diff->y) echo $diff->y . ' years<br>';
if($diff->m) echo $diff->m . ' months<br>';
if($diff->d) echo $diff->d . ' days<br>';
if($diff->h) echo $diff->h . ' hours<br>';
if($diff->i) echo $diff->i . ' minutes<br>';
if($diff->s) echo $diff->s . ' seconds<br>';

?>

I use a sort of IP banning because Im running this website as a mobi portal and every user got a unique ID I use as the ip in this case it is

$ip = $_SERVER["HTTP_X_MXIT_USERID_R"];

so that won't be a problem

 

The problem I'm having seems to be with part 1 the input of the time in my first post.

My database is bantime field is setuped as varchar 30

don't know if it would have any affect

Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/#findComment-1507365
Share on other sites

You get 1938 years because you are using a unix timestamp. If you do it that way you need

$dt1 = new DateTime();
$dt1->setTimestamp($expire);      // SET TIMESTAMP VALUE (RTFM)
$dt2=new DateTime(); // Now
$dif = $dt2->diff($dt1);
echo $dif->format('%y years, %m months, %d days, %h hours, %i minutes, %s seconds');

  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/#findComment-1507366
Share on other sites

 

You get 1938 years because you are using a unix timestamp. If you do it that way you need

$dt1 = new DateTime();
$dt1->setTimestamp($expire);      // SET TIMESTAMP VALUE (RTFM)
$dt2=new DateTime(); // Now
$dif = $dt2->diff($dt1);
echo $dif->format('%y years, %m months, %d days, %h hours, %i minutes, %s seconds');

ah thank you that solved my problem.

Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/#findComment-1507368
Share on other sites

Ok on the same topic I have another question

Lets say I kicked a user Permanently and insert into my field bantime the value PERMANENTLY. How can I go to do a while loop but if the one with the word PERMANENTLY appear it should display PERMANENTLY


$result = mysql_query("SELECT * FROM timeban WHERE `user` != ''",$conn);
 

if($result === FALSE) { die(mysql_error()); // TODO: better error handling } while($myrow = mysql_fetch_array($result)) { $ip = mysql_real_escape_string($myrow["ip"]); $expire = $myrow["bantime"]; $dt1 = new DateTime(); $dt1->setTimestamp($expire); // SET TIMESTAMP VALUE (RTFM) $dt2=new DateTime(); // Now $dif = $dt2->diff($dt1); echo $myrow["user"]. " - " .$dif->format('%m months, %d days, %h hours, %i minutes, %s seconds') . $banu;

at this moment I get the error

 

 

DateTime::setTimestamp() expects parameter 1 to be long, string given in

I know the reason is because it isn't in the right format but thats why I want it to display PERMANENTLY instead of the error in this case.

Edited by cobusbo
Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/#findComment-1507420
Share on other sites

You need to make up your mind whether that column should be a time stamp or not. You seem to want to throw anything into it.

 

Given what you have you need to check for 'PERMANENT' before you attempt any calculations on the value.

Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/#findComment-1507421
Share on other sites

You need to make up your mind whether that column should be a time stamp or not. You seem to want to throw anything into it.

 

Given what you have you need to check for 'PERMANENT' before you attempt any calculations on the value.

basically I just throw in my time but if its a permanent kick it replaces the the time with the word PERMANENTLY.

 

What I'm trying to accomplish is to show

 

 username - (Time left on the ban)

 

but the word Permanent change the outcome

 

I tried this

$result = mysql_query("SELECT * FROM timeban WHERE `user` != ''",$conn);
if($result === FALSE) { 
    die(mysql_error()); // TODO: better error handling
}

while($myrow = mysql_fetch_array($result))
{
$ip = mysql_real_escape_string($myrow["ip"]);
$expire = $myrow["bantime"];
if($expire === "PERMANENTLY") { 
$diff = "Permanently";    
$dt1 = new DateTime();
$dt1->setTimestamp($expire);      // SET TIMESTAMP VALUE (RTFM)
$dt2=new DateTime(); // Now
$dif = $dt2->diff($dt1);

print $myrow["user"]. " - " . $diff . $banu;

}else{

$dt1 = new DateTime();
$dt1->setTimestamp($expire);      // SET TIMESTAMP VALUE (RTFM)
$dt2=new DateTime(); // Now
$dif = $dt2->diff($dt1);
$diff = $dif->format('%m months, %d days, %h hours, %i minutes, %s seconds'); 

print $myrow["user"]. " - " . $diff . $banu;
}

but didn't help

Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/#findComment-1507424
Share on other sites

No, it wouldn't help much doing it like that. The reason for testing is so you DON'T do a calulation.

while($myrow = mysql_fetch_array($result))
{
$ip = mysql_real_escape_string($myrow["ip"]);          // WTF is this meant to achieve?
$expire = $myrow["bantime"];
if($expire === "PERMANENTLY") {
$diff = "Permanently";


print $myrow["user"]. " - " . $diff . $banu;

}else{

$dt1 = new DateTime();
$dt1->setTimestamp($expire); // SET TIMESTAMP VALUE (RTFM)
$dt2=new DateTime(); // Now
$dif = $dt2->diff($dt1);
$diff = $dif->format('%m months, %d days, %h hours, %i minutes, %s seconds'); 

print $myrow["user"]. " - " . $diff . $banu;
}

Why don't you use a DATETIME field. Then you can set a ban up to '9999-12-31 23:59:59', which is fairly permanent, and won't give you these problems with inconsistent data type in the same field

Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/#findComment-1507425
Share on other sites

No, it wouldn't help much doing it like that. The reason for testing is so you DON'T do a calulation.

while($myrow = mysql_fetch_array($result))
{
$ip = mysql_real_escape_string($myrow["ip"]);          // WTF is this meant to achieve?
$expire = $myrow["bantime"];
if($expire === "PERMANENTLY") {
$diff = "Permanently";


print $myrow["user"]. " - " . $diff . $banu;

}else{

$dt1 = new DateTime();
$dt1->setTimestamp($expire); // SET TIMESTAMP VALUE (RTFM)
$dt2=new DateTime(); // Now
$dif = $dt2->diff($dt1);
$diff = $dif->format('%m months, %d days, %h hours, %i minutes, %s seconds'); 

print $myrow["user"]. " - " . $diff . $banu;
}

Why don't you use a DATETIME field. Then you can set a ban up to '9999-12-31 23:59:59', which is fairly permanent, and won't give you these problems with inconsistent data type in the same field

That could work but how would I go to display Permanent instead of the time lets say if its bigger than 1 year rather display " Permanent" instead of the time when I echo it in the loop??

Edited by cobusbo
Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/#findComment-1507427
Share on other sites

  • Solution

Test the year diff. This assumes you set the bantime to '9999-12-31' if banned permanently


$expire = $myrow["bantime"];
$dt1 = new DateTime($expire);
$dt2=new DateTime(); // Now
$dif = $dt2->diff($dt1);
if ($dif->y > 1000) {        // if more than 1000 years
    $diff = 'PERMANENTLY';
} else {
    $diff = $dif->format('%m months, %d days, %h hours, %i minutes, %s seconds');
}
print $myrow["user"]. " - " . $diff . $banu;
Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/#findComment-1507428
Share on other sites

 

Test the year diff. This assumes you set the bantime to '9999-12-31' if banned permanently

$expire = $myrow["bantime"];
$dt1 = new DateTime($expire);
$dt2=new DateTime(); // Now
$dif = $dt2->diff($dt1);
if ($dif->y > 1000) {        // if more than 1000 years
    $diff = 'PERMANENTLY';
} else {
    $diff = $dif->format('%m months, %d days, %h hours, %i minutes, %s seconds');
}
print $myrow["user"]. " - " . $diff . $banu;

Ah thank you for all the help.

Link to comment
https://forums.phpfreaks.com/topic/295047-time-problem/#findComment-1507429
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.