'Change of td color with php if else statement [closed]
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>";
}
?>
Solution 1:[1]
$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>";
}
Solution 2:[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>";
}
Solution 3:[3]
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>";
}
?>
Solution 4:[4]
<!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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shailesh Katarmal |
| Solution 2 | |
| Solution 3 | xxx |
| Solution 4 | xxx |
