Nick Cotter Nick Cotter - 2 years ago 84
PHP Question

Time Checking in PHP

To start off I'm sorry for the noob question. Learning as I go but I'm having issues with this and couldn't fine the right search.

What I'm trying to accomplish:

  • I'm currently trying to combine strings of text and numbers for a ban list.

For example a person is banned for 40 minutes, In the SQL database it is stored as the value 40.That is fine when displayed in a table however issues arise when a person is banned for 10080 minutes (1 week)

What I have:

$sql = "SELECT name, authid, name, length FROM sb_ctbans";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
if ($row["length"] < '60'){
$row["length"] . "Minutes" == $ban['length'];
echo $ban['length'];
} else {
$row["length"] . "Undefined length" == $ban['length'];
echo "<tr>"."<td>" . $row["name"] ."</td>" . "<td>" . $row["authid"] ."</td>" . "<td>" . $ban['length'] . "</td>" ."</tr>";

Expected output: 2 Minutes
Actual output:

Cheers for any help.

After changing from:

$row["length"] . "Minutes" == $ban['length'];


` $ban['length'] = $row.['length'] . 'Minutes';`

I am now reviving the output
ArrayArray Minutes

Answer Source

You have a couple of problems here. You fixed the first wrong line, but it's again wrong at $row["length"] . "Undefined length" == $ban['length'];. Also, you're checking if the $row['length'] is less than the STRING '60', not the number 60, which could be causing errors. Finally, as others have said, storing the number of minutes to ban is sure to cause problems eventually. Instead, when the ban is first established, calculate the time it will be when the ban expires and store that instead, then when you check it, calculate how long til the time you stored. Otherwise as time passes, you must update every ban record every minute or they become inaccurate.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download