kevinfwb Posted February 8, 2007 Share Posted February 8, 2007 I have a PHP page that displays yearly escalations for properties that we lease. The PHP page shows 20 years of escalations that include the Start Date, End Date, Monthly Rent, and Annual Rent. Some properties only have 10 or 15 years worth of escalations with the remaining dates being null. When using the default MySQL, the date format it is displayed a YYYY-MM-DD. I've used the date format in PHP to show MM-DD-YYYY. Before using the date function, null values would show up as 0000-00-00. However, using the code below I get 11-30-1999 for null values instead of 00-00-0000. I would prefer to have the null value displayed instead of using an IF statement to omit them...they want to see all 20years of escalations even if they are blank. echo ' <td><div align="center">' . date ('m-d-Y', strtotime ($row['ts19'])) . "</div></td>\n"; Thanks -Kevin Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 8, 2007 Share Posted February 8, 2007 $time = strtotime ($row['ts19']); if($time){ echo ' <td><div align="center">' . date ('m-d-Y', $time) . "</div></td>\n"; }else{ echo ' <td> </td>\n"; } Quote Link to comment Share on other sites More sharing options...
kevinfwb Posted February 8, 2007 Author Share Posted February 8, 2007 Still showing it as 11-30-1999 ??? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 8, 2007 Share Posted February 8, 2007 Do this: $time = strtotime ($row['ts19']); if($time){ echo ' <td><div align="center">' . date ('m-d-Y', $time) . "</div></td>\n"; }else{ echo ' <td>'.$time.'</td>\n"; } What's it printing in those wrong ones? It should be nothing, or 0, right? Try changing the if to: if($time > 0){ Quote Link to comment Share on other sites More sharing options...
kevinfwb Posted February 8, 2007 Author Share Posted February 8, 2007 I've made both changes with the same result. I'm guessing that it is picking up the IF part of the statement and not the else. The output is still showing 11-30-1999 for MySQL values that have 0000-00-00. I would like those values to show up as 00-00-0000. Just FYI in the else line of your code you start with ' and end with " I changed the double to a single I did an echo $time and it displayed 378709210 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 8, 2007 Share Posted February 8, 2007 Please show us what's in $row['ts19']. The value of 378709210, when fed into date('m-d-Y') translates into 01-01-1982. Ken Quote Link to comment Share on other sites More sharing options...
kevinfwb Posted February 8, 2007 Author Share Posted February 8, 2007 There are 20 Rows of Escalations Term Start, Term End, Monthly Rent, Annual Rent ts1, te1, mr1, ar1 ts2, te2, mr2, ar2 through 20 For this particular entry only escalations 1 through 15 were entered. For the remaining 5 nothing was entered on the entry page. 0000-00-00 was not entered, it was just left blank. I'm not sure how MySQL handles null entries.. does it just zero them out? I can paste the entire code if needed. -Kevin Quote Link to comment Share on other sites More sharing options...
Hypnos Posted February 8, 2007 Share Posted February 8, 2007 The problem you're having is that 00-00-0000 is not a valid date, and can not be treated as such with the date functions. For instance: echo date('m-d-Y', strtotime(NULL)); Is kind of ok, and should echo todays date (because strtotime is returning NULL from NULL). But... echo date('m-d-Y', strtotime("0000-00-00")); ..is not ok. It will echo "11-30-1999" like you're getting. This is the solution I'll give you: <?php function flipdate($date) { if($date = "0000-00-00") return "00-00-0000"; return date('m-d-Y', strtotime($date)); } Once you place it at the top of your page, you should be able to use it like this: echo ' <td><div align="center">' . flipdate($row['ts19']) . "</div></td>\n"; And hopefully it will work how you want it to. Quote Link to comment Share on other sites More sharing options...
kevinfwb Posted February 8, 2007 Author Share Posted February 8, 2007 Worked like a charm. I thank everyone for their help! -Kevin 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.