trying to get boxes to show after 8 hrs

$b=array();

$selectTimeStampQuery = mysql_query("SELECT box1, box2, box3, box4, box5, box6, box7, box8, box9 FROM wallet3 WHERE uid='".$_SESSION['uid']."'");
$dateTimeCurr = date_create();
$dates = array();
while($row = mysql_fetch_assoc($selectTimeStampQuery)){
    $dates[] = $row;

}
if($dates[0]['box2'] == '0' && $dates[0]['box3'] == '0' && $dates[0]['box4'] == '0' && $dates[0]['box5'] == '0' && $dates[0]['box6'] == '0' && $dates[0]['box7'] == '0' && $dates[0]['box8'] == '0'){
    for($i=1;$i<9;$i++){
        $b[$i] = "N";
    }
    $dta = date_create($dates[0]['box1']);
    $dtDiff = $dateTimeCurr->diff($dta);
    $dtaFormat = $dtDiff->format("%a-%h");
    $dtaParts = explode('-', $dtaFormat);
    if($dtaParts[0] == '0'){
        if($dtaParts[1] > '8'){
            $b[0] = 'Y';
        }
        else{
            $b[0] = 'N';
        }
    }
    elseif($dtaParts[0] > '0'){
        $b[0] = 'Y';
    }
    else{
        $b[0] = 'N';
    }
}
else{
    $dates1 = array();
    foreach($dates[0] as $time){
        $dates1[] = date_create($time);
    }
    $diffArr = array();
    foreach($dates1 as $dt){
        if($dt == '0'){
            $diffArr[] = '0';
        }
        else{
            $dtt = $dateTimeCurr->diff($dt);
            $dttFor = $dtt->format("%a-%h");
            $dttParts = explode('-', $dttFor);
            $diffArr[] = $dttParts;
        }
    }
    for($i=0;$i<=8;$i++){
        if($diffArr[$i]=='0'){
            $b[$i] = 'N';
        }
        else{
            if($diffArr[$i][0] == '0'){
                if($diffArr[$i][1] > '8'){
                    $b[$i] = 'Y';
                }
                else{
                    $b[$i] = 'N';
                }
            }
            elseif($diffArr[$i][0] > '0'){
                $b[$i] = 'Y';
            }
        }
    }
}

}

This is supposed to show each of the nine boxes, one every 8 hours. but I am Always etting 8 of them showing in less than a day

First, please place your code inside the PHP tags. Just press the PHP button and place all of your code inside
or in-between them. It helps us very much when we transfer them to our own editors for testing. Thanks!

Now, not sure what you are asking. If you mean you want to show something after 8 hours, then you just
need to test the current time to see if it is past the 8 hour period. Here is an example that I used in a site to
test the profile date that forced users to update their profile every six months. This was needed for a legal issue
on a site I was helping with. Then, I show a sample of how to check for 8 hours. Is this what you need?

[php]
// Check the last time they visited their profile page. If over six months then go to profile page.
// If the date from the database is empty, they never filled in their profile, so it is needed…
if (is_null($row[“last_profile_update”]) OR ($row[“last_profile_update”]<=date(“Y-m-d H:i:s”, strtotime("-6 months")))) header(“Location: profile.php”);

// Check the date is at least 8 hours, if so display data.
while($row = mysql_fetch_assoc($selectTimeStampQuery)){
for($i=1; $i<9; $i++) {
if ($row[$i]<=date(“Y-m-d H:i:s”, strtotime("-8 hours"))) {
echo “box” . $i . ": " . $row[$i];
}
}
}
[/php]
The compare takes the current date, subtracts 8 hours from it and checks if the date in the row of data is from
before that altered date/time. Not very sure of the rest of your code. It is badly formed. Here are some notes
on things you should fix…

First, you need to dump all of the MySQL code and update it to MySQLi code. The old MySQL functions are no
longer used and will not be in your server at some point. Most programmers on this site suggest to move up
to PDO, but, since your code is simple, MySQLi should work well for you. It is easy to change up to the new
“improved” version of MySQLi…

Now, I am not sure why you would use a database set up with nine different boxes. This would indicate that
you did not design your database correctly. Normally, you would either place these boxes into an array or at
the least, just use one field for the box number. In this way, you would save several database entries, one for
each box number. The database query would be much easier, faster and so much easier to use when you do
the display of the results. But, you can work around the design by looping thru the boxes loaded in your way
with indexes. It would work… But, not the way databases are really designed to be used…

Well, hope that helps! I am sure you will have further questions on thsi…

Sponsor our Newsletter | Privacy Policy | Terms of Service