-3

I have a PHP with a list of renewal dates. The date field is "datetime".

How would i assign a style class / id to a row that had a date within 30 days of today ?

For example

Mr John Doe 31-03-2016 Mr John Test 31-05-2016

The top row would have a class of "renew". Here is the its so far

<?php   
$query = mysql_query("SELECT * FROM homepolicynumber order by id desc") 
or die(mysql_error()); 

echo "<table border='0' width='100%' cellpadding='10'>"; 
echo "<tr height='25px' valign='center' class='head'> 
    <th width='20px'><p></p></th> 
    <th width='140px'><p>Policy Number</p></th> 
    <th width='300px'><p>Customer Name</p></th> 
    <th width='115px'><p>Policy</p></th> 
    <th width='115px'><p>Date</p></th> 
    </tr>"; 

while($row = mysql_fetch_array( $query )) { 

    echo "<tr height='25px' valign='center'>"; 
    echo '<td valign="middle"><p><a href="delete.php?id=' . $row['id'] . '"><img src="../../Images/Icons/table-delete.png"/></a></p></td>'; 
    echo '<td><p>' . $row['brand'] . '-' . $row['publication'] . '-R-' . $row['id'] . '</p></td>'; 
    echo '<td><p>' . $row['title'] . '&nbsp;' . $row['firstname'] . '&nbsp;' . $row['lastname'] . '</p></td>'; 
    echo '<td><p>Home</p></td>'; 
    echo '<td><p>' . $row['datetime'] . '</p></td>'; 
    echo "</tr>"; 
} 

// close table> 
echo "</table>"; 
echo "<br /><p><b>View All</b> | <a class='link' href='view-all.php?page=1'>View Paginated</a></p>"; 
?> 
Ôrel
  • 7,044
  • 3
  • 27
  • 46
Grant
  • 9
  • 1
  • 5
  • Please add what you have done so far, it'll be much easier to help you. – Diego Bauleo Mar 17 '15 at 17:03
  • You need to compute the diff betwen now and your date, you can look here from the diff http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php/3923228#3923228 – Ôrel Mar 17 '15 at 17:26

2 Answers2

0
        while($row = mysql_fetch_array( $query )) {
            $class = (strToTime($row['datetime'])>(time()-60*60*24*30))?' class="theClass"':''
            echo "<tr height='25px' valign='center'" . $class . ">";
            echo '<td valign="middle"><p><a href="delete.php?id=' . $row['id'] . '"><img src="../../Images/Icons/table-delete.png"/></a></p></td>';
            echo '<td><p>' . $row['brand'] . '-' . $row['publication'] . '-R-' . $row['id'] . '</p></td>';
            echo '<td><p>' . $row['title'] . '&nbsp;' . $row['firstname'] . '&nbsp;' . $row['lastname'] . '</p></td>';
            echo '<td><p>Home</p></td>';
            echo '<td><p>' . $row['datetime'] . '</p></td>';
            echo "</tr>"; 
        } 
cyadvert
  • 855
  • 7
  • 19
0

Assuming that $row['datetime'] is in the future, try this:

<?php
    while($row = mysql_fetch_array( $query )) {
        $days_diff = strtotime($row['datetime']) - strtotime(date("Y-m-d H:i:s"));
        if($days_diff >= 30)
            echo "<tr height='25px' class='the-class' valign='center'>";
        else
            echo "<tr height='25px' valign='center'>";
        (...)
?>

I didn't test the code but it should work.

Diego Bauleo
  • 681
  • 3
  • 12