Logic Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Logic Problem

Post by shiznatix »

Ok so heres the deal, I have a start and end date. I need to set the background color of a row based on how close the current date is to the end date. I was thinking something like:

Code: Select all

$now = time();
$start = strtotime($start);
$end = strtotime($end);

$diff_seconds = $end - $start;

$diff = $diff_seconds / 10;

$current = $end - $now;
but then what? I have 10 color shades from 'you have time dont sweat it' to 'there is a fire in your pants' kinda deal. How do I decide what color to choose?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

I haven't worked with time stamps that much before, but I image this could be done with a pretty simple case statement.

Pseudo code:

Code: Select all

if timediff is > 0 then

switch (timediff):
  case > 2 weeks:
    colour=green;
    break;
  case > 1 week:
    colour=orange;
    break;
  case > 3 days:
    colour=red;
    break;
end

else

colour=red;

end if
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I'd agree with Jay. Colours are something that are very difficult to calculate and get good looking results every time. If there's only ten options I'd pick ten and use a switch case block.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

yes but the problem is the task can be any amount of time from a day to a month and it has to do as I had described.

What I could do is do it all based on the percent of time left. That just leaves the question, how can I get the percent of time left?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

find the difference between start date and end date. Get the difference between start date and now. All values are in the same scale now. Simple math:

Code: Select all

($now - $startDate) / ($endDate - $startDate)
The scale will be from 0 to 1 (floating point) .. multiply by 100 to get percent. ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

to further help along:

Code: Select all

$percent = round(10 * ($now - $startDate) / ($endDate - $startDate));
$aligned = max(0,min(10,$percent));
The number is now aligned to the nearest bracket 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, or 10 with a polarizing epsilon of 5%.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

lol, nice one feyd
Post Reply