Page 1 of 1
Logic Problem
Posted: Tue Feb 07, 2006 3:17 am
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?
Posted: Tue Feb 07, 2006 4:04 am
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
Posted: Tue Feb 07, 2006 6:09 am
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.
Posted: Tue Feb 07, 2006 9:24 am
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?
Posted: Tue Feb 07, 2006 9:26 am
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.

Posted: Tue Feb 07, 2006 9:41 am
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%.
Posted: Tue Feb 07, 2006 9:42 am
by malcolmboston
lol, nice one feyd