datoshway Posted January 19, 2011 Share Posted January 19, 2011 Need a little help. I have a date system where a date can be added or it can say TBA. The problem is if there is no date in the field it's displaying dec 31st 1969. Whats the best way if there is the date is tba we just display tba and not the 1969 business. heres my query // get dates $txtPageXML .= "\t<dates>\r\n"; $strQuery = sprintf( "SELECT d.strClinicDate FROM tblCitiesDates d LEFT JOIN tblCities c ON d.intCityID = c.intID WHERE c.intID = %d", intval($intCityID)); $queryGetDates = db_query($strQuery); if (db_num_rows($queryGetDates) > 0) { while ($objRow = db_fetch_object($queryGetDates)) { $strClinicDate = stripslashes($objRow->strClinicDate); if (strlen($strClinicDate) > 0) { $strClinicDate = date("M d, Y", strtotime($strClinicDate)); } $txtPageXML .= "\t\t<date value=\"" . $strClinicDate. "\" />\r\n"; } } db_free_result($queryGetDates); $txtPageXML .= "\t</dates>\r\n"; Quote Link to comment https://forums.phpfreaks.com/topic/224935-replace-all-timestamps-dec-31-1969/ Share on other sites More sharing options...
suresh_kamrushi Posted January 19, 2011 Share Posted January 19, 2011 Hey you can try this, // get dates $txtPageXML .= "\t<dates>\r\n"; $strQuery = sprintf( "SELECT IF( d.strClinicDate != NULL, d.strClinicDate, 0) as Cdate FROM tblCitiesDates d LEFT JOIN tblCities c ON d.intCityID = c.intID WHERE c.intID = %d", intval($intCityID)); $queryGetDates = db_query($strQuery); if (db_num_rows($queryGetDates) > 0) { while ($objRow = db_fetch_object($queryGetDates)) { $strClinicDate = stripslashes($objRow->Cdate); if (strlen($Cdate) != 0) { $strClinicDate = date("M d, Y", strtotime($strClinicDate)); } $txtPageXML .= "\t\t<date value=\"" . $strClinicDate. "\" />\r\n"; } } db_free_result($queryGetDates); $txtPageXML .= "\t</dates>\r\n"; I have updated select query and if statement. Quote Link to comment https://forums.phpfreaks.com/topic/224935-replace-all-timestamps-dec-31-1969/#findComment-1161801 Share on other sites More sharing options...
datoshway Posted January 19, 2011 Author Share Posted January 19, 2011 That didn't work, that put the date to 0 and got this error message. Notice: Undefined variable: Cdate in index.php on line 150 Quote Link to comment https://forums.phpfreaks.com/topic/224935-replace-all-timestamps-dec-31-1969/#findComment-1161898 Share on other sites More sharing options...
Pikachu2000 Posted January 19, 2011 Share Posted January 19, 2011 How are the dates actually stored now? In what format, and what is the data type of the table field they're in? Quote Link to comment https://forums.phpfreaks.com/topic/224935-replace-all-timestamps-dec-31-1969/#findComment-1161901 Share on other sites More sharing options...
datoshway Posted January 19, 2011 Author Share Posted January 19, 2011 Hey thanks for the quick reply. The dates are just varchar(255), they are formatted in the database like this: 2009-09-19 and the ones that are reading 1969 actually have TBA in the datafield which is what we want it to just out but it's giving me the 1969 instead Quote Link to comment https://forums.phpfreaks.com/topic/224935-replace-all-timestamps-dec-31-1969/#findComment-1161905 Share on other sites More sharing options...
Pikachu2000 Posted January 19, 2011 Share Posted January 19, 2011 You should always use a field type that's designed to store dates. It allows you to use the many native date/time functions native to MySQL, such as formatting, comparison, conversion, etc., and it's much more efficient. If you can't make that change, I suppose you could do a comparison in php when echoing the values. If the only thing in the field is 'tba', you can check for it and not allow the date() formatting to occur. Just echo 'tba' instead. This should get you pointed in the right direction, but seriously think about changing the say the dates are stored. if (strlen($strClinicDate) > 0 && intval($strClinicDate) > 0 ) { $strClinicDate = date("M d, Y", strtotime($strClinicDate)); } else { $strClinicDate = 'TBA'; } Quote Link to comment https://forums.phpfreaks.com/topic/224935-replace-all-timestamps-dec-31-1969/#findComment-1161912 Share on other sites More sharing options...
datoshway Posted January 19, 2011 Author Share Posted January 19, 2011 You rock man. Thank you so much. That worked perfectly. The only reason we aren't formatting the date field to actual date format is because of the variable of other items being stored like TBA in this case. Anyway. This worked really well for now, so thank you so much again for taking the time to help me out. Quote Link to comment https://forums.phpfreaks.com/topic/224935-replace-all-timestamps-dec-31-1969/#findComment-1161920 Share on other sites More sharing options...
Pikachu2000 Posted January 19, 2011 Share Posted January 19, 2011 There is one caveat with the above since there are values other than 'tba' and dates in there. If the value isn't a date, but starts with a digit or series of digits with a value greater than 0, it will not echo the TBA, it will try to echo a date from whatever the string is . . . Let me know if that's the case. Quote Link to comment https://forums.phpfreaks.com/topic/224935-replace-all-timestamps-dec-31-1969/#findComment-1161924 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.