-10

I want to change color of my td as per the date received from the sql query. Please help with following -

I want to change the color of td to green if the fetched date is less than today's date.

<?php
    if($mydate > date('d-M-Y'))
    {
        echo "<td style="background-color:green;">";
    }
    else
    {
       echo "<td>";
    }
?>
Nico Vignola
  • 479
  • 8
  • 16
Ramuk Tima
  • 25
  • 1
  • 8

4 Answers4

2

You cannot check dates in string formats.

One way you could do that is by formatting dates in UNIX timestamp (so that you have an integer) allowing you to check properly if a date is actually newer or older than another one.

$mydate = (new DateTime($mydate))->format('U');
$date   = (new DateTime(date('Y-m-d')))->format('U');

if($mydate > $date)
{
    echo "<td style='background-color: green;'>";
}
else
{
    echo "<td>";
}
GiamPy
  • 3,543
  • 3
  • 30
  • 51
1

First, convert your both time to a single format and compare

try following

I have used div instead of td for ease of use

<?php
    $mydate="08-sep-2015";
    //$mydate="11-sep-2015";
    $today     = date('d-M-Y');
    $todayTime = strtotime($today);
    $compTime  = strtotime($mydate);
    if($compTime < $todayTime) {
      echo "<div style='background-color:green;'>fgd</div>";
    }
    else{
      echo "<div>sdfsdf</div>";
    }
?>
xxx
  • 1,153
  • 1
  • 11
  • 23
Sarath
  • 608
  • 4
  • 12
  • @RamukTima you need to show us the code to see how you come up with `$mydate` – CodeGodie Sep 11 '15 at 09:47
  • @CodeGodie: [He mentioned it.](http://stackoverflow.com/questions/32520166/change-of-td-color-with-php-if-else-statement#comment52898083_32520166) – GiamPy Sep 11 '15 at 09:48
  • while($data=oci_fetch_array($array)) { $idno=$data[0]; $caseno=$data[1]; $pname=$data[2]; $rname=$data[3]; $purpose=$data[4]; $mydate=$data[5]; ?> – Ramuk Tima Sep 11 '15 at 09:50
  • @GiamPy he did mention it, but i think he is wrong since this is coming from the database, it would probably be stored as `2015-09-11` – CodeGodie Sep 11 '15 at 09:54
  • Updated the code please check it – Sarath Sep 11 '15 at 09:54
0
$mydate = "2015-10-01";
if(strtotime($mydate) > strtotime(date('Y-m-d')))
{
    echo "<td style='background-color:green;'>testing</td>";
}
else
{
   echo "<td>testing</td>";
}
Shailesh Katarmal
  • 2,757
  • 1
  • 12
  • 15
-1

<!DOCTYPE html>
<html>

<head>

</head>

<body>

  <table>

    <tr>

      <?php
        $mydate = "2015-10-01";
        if(strtotime($mydate) > strtotime(date('Y-m-d')))
        {
            echo "<td  bgcolor='#FF0000'>testing</td>";
        }
        else
        {
           echo "<td>NOt Green</td>";
        }
        ?>

    </tr>
  </table>

</body>

</html>
xxx
  • 1,153
  • 1
  • 11
  • 23
Subodh
  • 1
  • 3
  • You are wrong. http://jsfiddle.net/mplungjan/Lqh4m7bx/ – mplungjan Sep 11 '15 at 11:14
  • yes .Your right.Instead of should i use can not.Sorry for the type.My point that you should use bgcolor instead of background-color:.Any way thanx for your info. – Subodh Sep 11 '15 at 15:01
  • I meant: you are wrong. The style attribute contains css- it does exactly the same as bgcolor but using 21st century values. I did not refer to spelling mistake – mplungjan Sep 11 '15 at 15:05
  • The HTML bgcolor attribute was deprecated back in 1997 (in W3C HTML 4.0); you should use the CSS background-color style instead. – ro͢binmckenzie Sep 14 '15 at 07:42