Guest KhAoZ Posted July 3, 2006 Share Posted July 3, 2006 I basically have written a date formatter for mysql time stamps. However, there is a problem with it: random strings appear in the final results, and I cannot find the cause of them.Here is the orignal parsing code:[code] function parseDate($date) { //$date example: 2006-07-03 01:02:26 $year = substr($date, 0, 4); $month = substr($date, 5, 2); $day = substr($date, 8, 2); $hour = substr($date, 11, 2); $minutes = substr($date, 14, 2); switch ($month) { case "01": $month = "January"; break; case "02": $month = "Febuary"; break; case "03": $month = "March"; break; case "04": $month = "April"; break; case "05": $month = "May"; break; case "06": $month = "June"; break; case "07": $month = "July"; break; case "08": $month = "August"; break; case "09": $month = "September"; break; case "10": $month = "October"; break; case "11": $month = "November"; break; case "12": $month = "December"; break; } if ($hour > 12) { $hour -= 12; $hour2 = "pm"; } else { $hour2 = "am"; } if (substr($day, 0, 1) == "0") { $day = substr($day, 1, 1); } echo "$month $day, $year $hour:$minutes"; }[/code]The output from the $date example above is: [code]July 3, 2006 01:02 , :[/code]The " , :" appears there for no reason. I then began to play with the code, and it got even wierder!I changed the echo to a return[code]echo "$month $day, $year $hour:$minutes";[/code]to[code]return "$month $day, $year $hour:$minutes";[/code]And had the following result:[code]3, 20, July 6 :1:[/code]Now that is pretty intresting, no?And with the first attempt (with the echo), if you remove the last variable, $minutes, you get the following output:[code]July 3, 2006 01 ,[/code]Now that is even more awesome. Notice that the only thing different from that result, and the one with $minutes, is just the removal of the semicolon, yet the comma stays there even though it was in the middle of the semilicolon and the minutes variable.Can someone please try and run this code to see if they get the same result?? Cuz I am absolutly stumpted, because it all seems really really random. I think this code did [i]once[/i] work a while ago (on different version etc), and it may be some php settings or something.Thanks a lot for any help. Quote Link to comment https://forums.phpfreaks.com/topic/13523-a-really-really-wierd-problem-different-results-from-return-and-echo/ Share on other sites More sharing options...
Brandon Jaeger Posted July 3, 2006 Share Posted July 3, 2006 Try adding this:[code=php:0] $timestr = ""; $timestr .= ($month) ? " " . $month : ""; $timestr .= ($day) ? " " . $day : ""; $timestr .= ($year) ? ", " . $year : ""; $timestr .= ($hour) ? " " . $hour : ""; $timestr .= ($minutes) ? ":" . $minutes : ":00"; echo $timestr;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13523-a-really-really-wierd-problem-different-results-from-return-and-echo/#findComment-52363 Share on other sites More sharing options...
.josh Posted July 3, 2006 Share Posted July 3, 2006 just out of curiosity, is there any reason why you aren't using the [url=http://www.php.net/date]date()[/url] function in conjunction with the [url=http://www.php.net/mktime]mktime()[/url] function to build a time format of your choosing? Quote Link to comment https://forums.phpfreaks.com/topic/13523-a-really-really-wierd-problem-different-results-from-return-and-echo/#findComment-52365 Share on other sites More sharing options...
KhAoZ Posted July 3, 2006 Share Posted July 3, 2006 Brandon Jaeger:Yeah, with that code the echo works, but still, when I attempt to return the string, I get:[code]March 2, Jul 06:01[/code]And basically, the calling function is just doing:[code]echo parseDate($date);[/code]Any idea's why returning the string is messing it all up?EDIT:Nevermind, it still doesn't work. The output is:[code] July 3, 2006 01:02:00[/code]For some reason, an extra :00 is added at the end... Quote Link to comment https://forums.phpfreaks.com/topic/13523-a-really-really-wierd-problem-different-results-from-return-and-echo/#findComment-52469 Share on other sites More sharing options...
KhAoZ Posted July 5, 2006 Share Posted July 5, 2006 Doens't seem like anyone has any ideas as to what is wrong.Anyone think this might be a bug? Can anyone try this out on their computer? Quote Link to comment https://forums.phpfreaks.com/topic/13523-a-really-really-wierd-problem-different-results-from-return-and-echo/#findComment-53452 Share on other sites More sharing options...
kenrbnsn Posted July 5, 2006 Share Posted July 5, 2006 Why don't you use the PHP functions date() and strtotime() that are designed to handle dates?[code]<?php$date = '2006-07-03 01:02:26';echo date('F j, Y g:i a', strtotime($date));?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/13523-a-really-really-wierd-problem-different-results-from-return-and-echo/#findComment-53469 Share on other sites More sharing options...
KhAoZ Posted July 6, 2006 Share Posted July 6, 2006 I can and I do.However, for fun, I just wrote that small thing, and ecountered some wierd problems. I want to know whats going on with that code. The problem is not getting a date formatter to work, its that the echo function does something different than what is returned by the return function. And their both the wrong result anyway... Quote Link to comment https://forums.phpfreaks.com/topic/13523-a-really-really-wierd-problem-different-results-from-return-and-echo/#findComment-53958 Share on other sites More sharing options...
kenrbnsn Posted July 6, 2006 Share Posted July 6, 2006 I just tried your original code and got the correct answer using both echo and return.Ken Quote Link to comment https://forums.phpfreaks.com/topic/13523-a-really-really-wierd-problem-different-results-from-return-and-echo/#findComment-53989 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.