I have mySQL tables that are suffixed with 'mmdd'. I need to iterate through and store this 'mmdd' value in an array as I get a hit on the table. The problem is that the date subtract of 1 day only works on the 1st iteration. I have tried this many ways - As a function in my class, etc; but, it only subtracts the day 1 time. Gotta be a better way.
$i = 1;
$newday = array();
if ($current === 'Y') {
$newday[0] = $datesplit[1];
do {
$sqlstmt = "select * from dbname.mytable_".$newday[$i-1];
$resultchk = $db->query($sqlstmt);
$dtset = date($newday[$i-1]);
$newday[$i] = date('md', strtotime($dtset.' -1 day'));
$i++;
} while(mysqli_num_rows($resultchk) > 0);
It's generally a lot better to use the DateTime class in PHP if you want to manipulate dates.
Assuming that $datesplit[1]
contains something like '0806'
(for August 6), I would change your code to something like this:
$i = 0;
$newday = array();
$dtset = DateTime::createFromFormat('md', $datesplit[1]);
if ($current === 'Y') {
do {
$newday[$i] = $dtset->format('md');
$sqlstmt = "select * from dbname.mytable_".$newday[$i];
$resultchk = $db->query($sqlstmt);
$dtset->sub(new DateInterval('P1D'));
$i++;
} while(mysqli_num_rows($resultchk) > 0);
}