liam1964 Posted February 3, 2010 Share Posted February 3, 2010 I am feeling really stupid asking this question, but I can't find a solution. I have a database (MySQL) with a few date fields in three tables. MySQL defaults to 0000-00-00 when no date is entered. However if the date in the table is 0000-00-00 I want some PHP code that will return nothing, and only show a date if the value is anything other than 0000-00-00. I thought something like this would work, but :-( if $date=='0000-00-00' return ' ' Can anybody please help me. I am new to PHP so PLEASE don't just tell me "how" to do it, I will not understand. But if I can see some code, I am capable of adapting it to suit my needs (I hope). Thanks in advance!! Quote Link to comment https://forums.phpfreaks.com/topic/190740-if-date-is-0000-00-00-i-want-php-to-return-nothing/ Share on other sites More sharing options...
teamatomic Posted February 3, 2010 Share Posted February 3, 2010 if ($date == '0000-00-00') {$date='';} HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/190740-if-date-is-0000-00-00-i-want-php-to-return-nothing/#findComment-1005881 Share on other sites More sharing options...
salathe Posted February 3, 2010 Share Posted February 3, 2010 Is there any particular reason why the default is 0000-00-00? If you allow the column to have NULL values in the table, you won't have to use PHP to look for the special zero date. Quote Link to comment https://forums.phpfreaks.com/topic/190740-if-date-is-0000-00-00-i-want-php-to-return-nothing/#findComment-1006070 Share on other sites More sharing options...
liam1964 Posted February 3, 2010 Author Share Posted February 3, 2010 Is there any particular reason why the default is 0000-00-00? If you allow the column to have NULL values in the table, you won't have to use PHP to look for the special zero date. Before I open my big mouth, and make a fool of myself, let me say again, I am NO expert. Apparently, this some kind of bug with Mysql, if you set DATE to NULL, the NULL value is 0000-00-00. ??????? Quote Link to comment https://forums.phpfreaks.com/topic/190740-if-date-is-0000-00-00-i-want-php-to-return-nothing/#findComment-1006088 Share on other sites More sharing options...
liam1964 Posted February 3, 2010 Author Share Posted February 3, 2010 if ($date == '0000-00-00') {$date='';} HTH Teamatomic This did the trick ---- THANK YOU ... so simple when you know how, but I love to learn. Now I am hitting my head against another brick wall ... formatting the date. if ($row['date_call']==''); $date = $row['date_call']; if ($date == '0000-00-00') {$date='';} How do I then go on to say, if the date is not equal to 0000-00-00, format it (m-d-Y) - or should I be doing this first. Quote Link to comment https://forums.phpfreaks.com/topic/190740-if-date-is-0000-00-00-i-want-php-to-return-nothing/#findComment-1006092 Share on other sites More sharing options...
MatthewJ Posted February 3, 2010 Share Posted February 3, 2010 if ($date == '0000-00-00') { $date=''; } else { $parts = explode('-', $date); $date = date('m-d-Y', mktime(0,0,0,$parts[1],$parts[2],$parts[0])); } Quote Link to comment https://forums.phpfreaks.com/topic/190740-if-date-is-0000-00-00-i-want-php-to-return-nothing/#findComment-1006101 Share on other sites More sharing options...
liam1964 Posted February 3, 2010 Author Share Posted February 3, 2010 Very interesting ... am not even sure I understand what that is trying to do, but I now have: if ($row['date_call']==''); $date = $row['date_call']; if ($date == '0000-00-00') { $date=''; } else { $parts = explode('-', $date); $date = date('m-d-Y', mktime(0,0,0,$parts[1],$parts[2],$parts[0])); } and that gives me this error - Warning: mktime() expects parameter 6 to be long, string given in /home/***/public_html/***/calls_1.php on line 236 Hmmm - *Ponders* Quote Link to comment https://forums.phpfreaks.com/topic/190740-if-date-is-0000-00-00-i-want-php-to-return-nothing/#findComment-1006118 Share on other sites More sharing options...
MatthewJ Posted February 3, 2010 Share Posted February 3, 2010 //This if is not doing anything because you end it with a semicolon //Should be curly braces surrounding the code to execute if true //so technically you were checking if date call was equal to blank //but then were setting $date equal to it no matter what //so when you ran that, it passed in a blank value for $date to the explode //function which means parts[x] were all empty if ($row['date_call']=='') { $date = $row['date_call']; } if ($date == '0000-00-00') { $date=''; } else { //Separate the date parts into an array. $parts[0] would be year //$parts[1] would be month and $parts[2] would hold the day. $parts = explode('-', $date); //Format the date the way you want (m-d-y) based on the timestamp value //returned by mktime() which is basically mktime(hour, minute, second, month, day, year) $date = date('m-d-Y', mktime(0,0,0,$parts[1],$parts[2],$parts[0])); } Quote Link to comment https://forums.phpfreaks.com/topic/190740-if-date-is-0000-00-00-i-want-php-to-return-nothing/#findComment-1006159 Share on other sites More sharing options...
aeroswat Posted February 3, 2010 Share Posted February 3, 2010 if ($date == '0000-00-00') { $date=''; } else { $parts = explode('-', $date); $date = date('m-d-Y', mktime(0,0,0,$parts[1],$parts[2],$parts[0])); } Or you could format it in your mysql query. (I hear this is a better way) I.e make your query be something like this SELECT *, DATE_FORMAT(Date,'%m-%d-%Y') as FixedDate FROM table Quote Link to comment https://forums.phpfreaks.com/topic/190740-if-date-is-0000-00-00-i-want-php-to-return-nothing/#findComment-1006162 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.