Jump to content

Change color to table background after certain time


badeand

Recommended Posts

Hi,

 

I've making a script that's almost a todo list, and i want to have 3 colors to the posts.

 

Post under 7 days old should be green, and thoose older then 7 days yellow and over 14 days should be red.

How could i do that? Every post i timestamp when stored in my MYSQL table.

 

Here is my code:


<?php
$servername='localhost';


$dbusername='root';
$dbpassword='';


$dbname='store';



connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}


$result = mysql_query("SELECT * FROM henvendelser ORDER by id desc ") 
or die(mysql_error());  

echo "<table cellspacing='12px' cellpaddomg='5px' align='center'>";
echo "<tr> <th>ID</th> <th> Opprettet </th> <th>Navn</th> <th>Telefon</th>  <th>Emne</th> </tr>";

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





echo "<tr>

<td>";
echo $row['id'];
echo "</td>

<td>";
echo date("d.m.y", strtotime($row["date"]));
echo "</td>

<td>"; 
echo "<a href=\"detaljer.php?view=$row[id]\">$row[Navn]</a>";
echo "</td>
    
<td>"; 
echo $row['Telefon'];
echo "</td>


<td>"; 
echo $row['Emne'];
echo "</td>


</tr>"; 
} 

echo "</table>";


?>

Link to comment
Share on other sites

You want to compare your date row in the database with the current date, using timestamps, so strtotime the date row, and then  subtract the date row from the current timestamp. This is done in seconds, so do how many seconds in 7 days and 14 days, and if greater than 14 days (in seconds) set the style color to red, elseif greater than 7 set it to yellow, else set it to green.

Link to comment
Share on other sites

If I understand your question right, the rest is just math...

 


$timestamp; // The timestamp you've got from database.
$distance = time() - $timestamp;

$font['color'] = "black";
if ($distance < (60*60*24*7)) // under 7 days
    $font['color'] = "green";
elseif ($distance > (60*60*24*14)) // over 14 days
    $font['color'] = "red";
else // between 7 and 14 days
    $font['color'] = "yellow";

 

Quote php manual: "Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)"

So all you have to do is multiply the amount of seconds in a minute with the minutes in an hour with the hours in a day and in the end with the number of days ;)

Link to comment
Share on other sites

Not a fan of doing it the font color way, but this is an idea, and it should work, untested though.

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

$datePosted   = strtotime($row["date"]);
$dateNow      = time();

$howLongSince = $dateNow - $datePosted;

/*
60*60*24*7   = 1 week
60*60*24*7*2 = 2 weeks
*/

if ($howLongSince>60*60*24*7*2) // Older than 2 weeks
{
	$color = "red";
} elseif ($howLongSince>60*60*24*7) // Older than 1 week, less than 2 weeks
{
	$color = "yellow";
} else {
	$color = "green";
}

echo "<font color=\"$color\">";


echo "<tr>

<td>";
echo $row['id'];
echo "</td>

<td>";
echo date("d.m.y", strtotime($row["date"]));
echo "</td>

<td>"; 
echo "<a href=\"detaljer.php?view=$row[id]\">$row[Navn]</a>";
echo "</td>
    
<td>"; 
echo $row['Telefon'];
echo "</td>


<td>"; 
echo $row['Emne'];
echo "</td>


</tr>"; 


echo "</font>"; // END FONT
} 

echo "</table>";

 

EDIT: Groon, did you seriously beat me by 11 seconds?

Link to comment
Share on other sites

Badeand, php doesn't recognize the code as red, because php is an interpreted scripting language, that works server-side, meaning: it doesn't actually touch the actual markup at any point. So it is your browser which recognizes the color code, and i do recommend using hex colors, giving you the control of which color to see.

 

The reason why me and Zurev used the nickname for the color is because it looks better when we're showing it to you (not meaning it doesn't work, it is just not recommended).

 

 

EDIT: Groon, did you seriously beat me by 11 seconds?

Sometimes the world is just mean xD

Link to comment
Share on other sites

I've changed it to HEX colors but it still dosent change the font color.

 

What could it be?

 

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

$datePosted   = strtotime($row["date"]);

$dateNow      = time();

$howLongSince = $dateNow - $datePosted;

/*

60*60*24*7   = 1 week



60*60*24*7*2 = 2 weeks

*/
if ($howLongSince>60*60*24*7*2) // Older than 2 weeks

{

$color = "#D1180A";

} elseif ($howLongSince>60*60*24*7) // Older than 1 week, less than 2 weeks	

{

$color = "#D1B30A";

} 

else {
$color = "#08C90F";
}

echo "<font color=\"$color\">";
echo "<tr>

<td>";
echo $row['id'];
echo "</td>

<td>";
echo date("d.m.y", strtotime($row["date"]));
echo "</td>


<td>"; 
echo "<a href=\"detaljer.php?view=$row[id]\">$row[Navn]</a>";
echo "</td>
    

<td>"; 
echo $row['Telefon'];
echo "</td>


<td>"; 
echo $row['Emne'];
echo "</td>

</tr>"; 


echo "</font>"; // END FONT
} 

echo "</table>";
            ?>

Link to comment
Share on other sites

I have to highlight that I did at no point in my reply, state that it would fix your problem to change it to hexcode ^^,  I just came with a tip that you should normally use hexcode ;)

 

I can't find any problems with the syntax right now, try to give me some feed back.

Check the value of $color, and the value of the timestamp, to see if they are as you expect them :)

Link to comment
Share on other sites

Hi again :)

 

I'm kinda new to this, i really dont know how to check the value of $color.

 

Isn't the value the different hex codes?

 

I've attached a picture of my mysql table with the timestamp if that is any help.

 

I really appriciate all your help and guidance :)

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Hi again :)

 

I'm kinda new to this, i really dont know how to check the value of $color.

 

Isn't the value the different hex codes?

 

I've attached a picture of my mysql table with the timestamp if that is any help.

 

I really appriciate all your help and guidance :)

 

Don't know how to check the value of color?

echo $color;

 

Check it and timestamp during the loop and see what you come back with.

Link to comment
Share on other sites

I apologize if I am imposing but try the following :)

 

<?php
  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 = '#08C90F';
    } 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 = '#D1180A';
    }

    echo '<tr style="color: '.$color.';">';

    echo '<td>'. $row['id'] .'</td>';

    echo '<td>'. date('d.m.y', $postTime) .'</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>';
?>

 

*This assumes you use the timestamp of "time()" when adding a record, otherwise change the formatting above of "$postTime" to make sure it's in the format of "1234567890"

 

Regards, PaulRyan  :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.