Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Eka
    Eka is offline
    New to the CF scene
    Join Date
    May 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Color fading code according to Unix time

    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!
    Code:
    $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!

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    hth, you can mess with the initial parameters to fine tune the effect:

    PHP Code:
    $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(0100000))
    {
        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 />';  



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •