...

View Full Version : Color fading code according to Unix time



Eka
05-04-2006, 02:29 PM
What I have here is a silly piece of code. Why is it silly? I know little to nothing about programming, It ought to be silly!


$currentt = time();
echo $info_message;
if (count($u_ids))
{
echo "$w_search_results<br>";
echo "<table>";
for ($i=0;$i<count($u_ids);$i++)
{
echo "<tr><td><a href=\"fullinfo.php?user_id=".$u_ids[$i]."&session=$session\">".$u_names[$i]."</a></td> <td>\n";
$diffe = $currentt - $last_visit[$i];
if ($diffe < 1000000)
echo "<font color=00ffff>";
else if ($diffe < 2000000)
echo "<font color=00eeee>";
else if ($diffe < 3000000)
echo "<font color=00dddd>";
else if ($diffe < 4000000)
echo "<font color=00cccc>";
else if ($diffe < 5000000)
echo "<font color=00bbbb>";
else if ($diffe < 6000000)
echo "<font color=00aaaa>";
else if ($diffe < 7000000)
echo "<font color=009999>";
else if ($diffe < 8000000)
echo "<font color=008888>";
else if ($diffe < 9000000)
echo "<font color=007777>";
else
echo "<font color=006666>";
print "Last Visited: <font face=Courier>" . date("M-d-Y H:i:s ", $last_visit[$i]);


What I'm trying to do here, is to make the color of the text go from brightest to dimest according to the last time they visit. $last visit respersent the unix time code of the last time they log in, and this code basically output everyone's name and the time they login.

How do I improve on this? Surely I want more color shade then the number if IF statment allow, but thinking about how to translate the $diffe, as in the number of seconds between now and the last time they login, into a proper format that font color will use, is giving me a hard time.

Sorry if this seem beginner, but I have no PHP courses and other programming courses. Just want to start impoving code and learn from there.

Thank you in advance for your advice!

ralph l mayo
05-04-2006, 06:23 PM
hth, you can mess with the initial parameters to fine tune the effect:



$smallest['timediff'] = 1000000;
$smallest['shade'] = 'ff'; // just one block (dec 0-255)
$largest['timediff'] = 9000000;
$largest['shade'] = '66';

// loop is just to demonstrate, you still want to calculate the diff the way you do now.
for ($diff = $smallest['timediff']; $diff <= $largest['timediff']; $diff += mt_rand(0, 100000))
{
if ($diff <= $smallest['timediff'])
{
$shade = '#00' . str_repeat($smallest['shade'], 2);
}
else if ($diff >= $largest['timediff'])
{
$shade = '#00' . str_repeat($largest['shade'], 2);
}
else
{
// the difference expressed as a percentage of the time interval
$per = (($diff - $smallest['timediff']) * 100)/($largest['timediff'] - $smallest['timediff']);
// the color interval
$shadediff = hexdec($largest['shade']) - hexdec($smallest['shade']);
// the smallest color + the percentage of the color interval equivalent to the percentage of the time interval
$shade = '#00' . str_repeat(dechex(hexdec($smallest['shade']) + ($shadediff * $per / 100)), 2);
}
echo '<span style="color: ' . $shade . ';">' . $shade . '</span><br />';
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum