pauldavidbrown Posted January 9, 2016 Share Posted January 9, 2016 I'm sure this is super simple, I just can't figure it out. I have a php page that accesses a mysql database and shows the date closed. I simply want the page to display the date in black by default, green if 30-60 days past (from current date), orange if 60-90, and red if over 90. The Structure of the CloseDate field is just a date field within mysql. This runs on a bluehost server with all current php and mysql versions. Any response is sincerely appreciated. See attached screen shot. Quote Link to comment Share on other sites More sharing options...
benanamen Posted January 9, 2016 Share Posted January 9, 2016 What have you tried? Where is your code? Quote Link to comment Share on other sites More sharing options...
pauldavidbrown Posted January 9, 2016 Author Share Posted January 9, 2016 (edited) Most recently, I have tried this... $Today = date('m/d/Y'); $Days = date_diff($Today, $row('CloseDate')); echo $Days->format('%R%a days'); But -- I cannot get it to even display the # of days in that format and am therefore stuck. I'm guessing it is because the two are not in the same date format. I've also tried $Today = new DateTime("now"); -- but it yields the same results. Edited January 9, 2016 by pauldavidbrown Quote Link to comment Share on other sites More sharing options...
Barand Posted January 9, 2016 Share Posted January 9, 2016 How are you storing the date in your db table? It should be type DATE, format YYYY-MM-DD. Quote Link to comment Share on other sites More sharing options...
pauldavidbrown Posted January 9, 2016 Author Share Posted January 9, 2016 The format in mysql is YYYY-MM-DD Quote Link to comment Share on other sites More sharing options...
Barand Posted January 10, 2016 Share Posted January 10, 2016 here's an example <?php $dates = [ '2015-09-12', '2015-10-15', '2015-11-23', '2015-12-08', '2015-12-28' ]; $now = new DateTime(); $tdata = ''; foreach ($dates as $dt) { $closed = new DateTime($dt); $age = $now->diff($closed)->days; $dateout = $closed->format('m/d/Y'); if ($age > 90) { $class = 'over90'; } elseif ($age > 60) { $class = 'over60'; } elseif ($age > 30) { $class = 'over30'; } else $class = 'default'; $tdata .= "<tr><td class='$class'>$dateout</td></tr>"; } ?> <html> <head> <title>Example</title> <style type='text/css'> td.default {color: black;} td.over30 {background-color: green; color: white;} td.over60 {background-color: orange; color: white;} td.over90 {background-color: red; color: white;} </style> </head> <body> <table> <?=$tdata?> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.