I'm having trouble with the date comparison loop of our project. it seems like it is not reading the syntax properly. The DonationDate needs to be compared to DonationDate. '+ 90 Days'. if the DonationDate is less than or equal to DonationDate. ' + 90 Days'. the result should be replenishing but I am getting active donors even if the input date is today.
Here is the code:
<?php $sql = "SELECT * from tblblooddonars WHERE status='Donated' ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{?>
<tr>
<td style="color:black;font-size:18px;"><?php echo htmlentities($cnt);?>
</td>
<td>
<span style="color:black;font-size:18px;"><?php echo
htmlentities($result->LastName);?></span>
<span style="color:black;font-size:18px;"><?php echo
htmlentities($result->FirstName);?></span>
<span style="color:black;font-size:18px;"><?php echo
htmlentities($result->MiddleInitial);?></span>
</td>
<td>
<span style="color:black;font-size:18px;"><?php echo
htmlentities($result->BloodGroup);?></span>
</td>
<td>
<span style="color:black;font-size:18px;"><?php echo date('M d,
Y',strtotime($result->Birthdate));?></span>
</td>
<td>
<span style="color:black;font-size:18px;"><?php echo date('M d,
Y',strtotime($result->DonationDate));?></span>
</td>
<?php
if ($result->status=='Approve'
&& date('M d, Y',strtotime($result->DonationDate)) <= date('M d,
Y',strtotime($result->DonationDate. '+ 90 Days'))
)
{ ?>
<td style="max-width:50px;">
<span style="color:black;font-size:18px;"><label
style="color:red">Replenishing</a></span>
</td>
<?php
} else {
?>
<td style="max-width:50px;">
<span style="color:black;font-
size:18px;"><label style="color:green">Active Donor</a></span>
</td>
<?php
}
?>
</tr>
<?php
$cnt=$cnt+1;
}
?>
<?php
}?
>
My Answer:
php
$DonationDate = '2022-01-01';
$today = date('Y-m-d');
$thresholdDate = date('Y-m-d', strtotime('-90 days'));
if ($DonationDate <= $thresholdDate) {
echo 'replenishing';
} else {
echo 'active donors';
}
In the code above, we are comparing the `$DonationDate` with a threshold date that is 90 days in the past. If the `$DonationDate` is less than or equal to the threshold date, it will output 'replenishing', otherwise it will output 'active donors'.
Make sure that the format of the dates is correct and that the comparison is being done properly. If you are still facing issues, you may need to check the format of the dates and ensure that they are being compared correctly.
Rate this post
3 of 5 based on 1055 votesComments