badeand Posted January 14, 2011 Share Posted January 14, 2011 Hi people I'm trying to show all my data in my database, and I'm trying to make the font color change after 7 days to yellow and after 14 days it should show up red. But it does not work, could someone see any error in the code? It turns up green all the time.. while($row = mysql_fetch_array($result)) { $postTime = $row['date']; $thisTime = time(); $timeDiff = $thisTime-$postTime; if($timeDiff <= 604800) { // Less than 7 days[60*60*24*7] $color = '#D1180A'; } else if($timeDiff > 604800 && $timeDiff <= 1209600) { // Greater than 7 days[60*60*24*7], less than 14 days[60*60*24*14] $color = '#D1B30A'; } else if($timeDiff > 1209600) { // Greater than 14 days[60*60*24*14] $color = '#08C90F'; } echo '<tr style="color: '.$color.';">'; echo '<td>'. $row['id'] .'</td>'; echo '<td>'. date("d.m.y", strtotime($row["date"])) .' </td>'; echo '<td><a href="detaljer.php?view='. $row['id'] .'">'. $row['Navn'] .'</a></td>'; echo '<td>'. $row['Telefon'] .'</td>'; echo '<td>'. $row['Emne'] .'</td>'; echo '</tr>'; } echo '</table>'; Quote Link to comment https://forums.phpfreaks.com/topic/224431-problem-with-changing-font-color-after-certain-time/ Share on other sites More sharing options...
BlueSkyIS Posted January 14, 2011 Share Posted January 14, 2011 echo $postTime; echo $thisTime; echo $timeDiff; see what they are. we can't tell what they are from here. Quote Link to comment https://forums.phpfreaks.com/topic/224431-problem-with-changing-font-color-after-certain-time/#findComment-1159362 Share on other sites More sharing options...
badeand Posted January 14, 2011 Author Share Posted January 14, 2011 This is the value of thoose variables 2011-01-01 16:02:42 1295019624 1295017613 Quote Link to comment https://forums.phpfreaks.com/topic/224431-problem-with-changing-font-color-after-certain-time/#findComment-1159370 Share on other sites More sharing options...
The Little Guy Posted January 14, 2011 Share Posted January 14, 2011 I would take an approach like this: if($postTime < strtotime("now +7 days")){ // less than 7 days }else{ // more than 7 days } if($postTime > strtotime("now +14 days")){ // more than 14 days } Quote Link to comment https://forums.phpfreaks.com/topic/224431-problem-with-changing-font-color-after-certain-time/#findComment-1159378 Share on other sites More sharing options...
BlueSkyIS Posted January 14, 2011 Share Posted January 14, 2011 This is the value of thoose variables 2011-01-01 16:02:42 1295019624 1295017613 ... and there lies the problem. 2011-01-01 16:02:42 is a date/time while 1295019624 is a timestamp. you can't perform addition/subtraction on these 2 values without converting one type to the other. you can use strtotime() to convert a date/time to a timestamp. But on the other hand, The Little Guy has a possibly simpler solution. Quote Link to comment https://forums.phpfreaks.com/topic/224431-problem-with-changing-font-color-after-certain-time/#findComment-1159381 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.