Jump to content


A really really wierd problem, different results from return and echo


  • Please log in to reply
7 replies to this topic

#1 Guest_KhAoZ_*

Guest_KhAoZ_*
  • Guests

Posted 03 July 2006 - 06:31 AM

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:

	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";
		
		
	}

The output from the $date example above is:
July 3, 2006 01:02 , :

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
echo "$month $day, $year $hour:$minutes";
to
return "$month $day, $year $hour:$minutes";

And had the following result:
3, 20, July 6 :1:

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:
July 3, 2006 01 ,

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 once work a while ago (on different version etc), and it may be some php settings or something.

Thanks a lot for any help.


#2 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 03 July 2006 - 06:39 AM

Try adding this:
$timestr = "";

		$timestr .= ($month) ? " " . $month : "";
		$timestr .= ($day) ? " " . $day : "";
		$timestr .= ($year) ? ", " . $year : "";
		$timestr .= ($hour) ? " " . $hour : "";
		$timestr .= ($minutes) ? ":" . $minutes : ":00";

		echo $timestr;

PHP is my friend (=

#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 03 July 2006 - 06:44 AM

just out of curiosity, is there any reason why you aren't using the date() function in conjunction with the mktime() function to build a time format of your choosing?


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 KhAoZ

KhAoZ
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 July 2006 - 03:17 PM

Brandon Jaeger:

Yeah, with that code the echo works, but still, when I attempt to return the string, I get:
March 2, Jul 06:01

And basically, the calling function is just doing:
echo parseDate($date);

Any idea's why returning the string is messing it all up?

EDIT:

Nevermind, it still doesn't work. The output is:
July 3, 2006 01:02:00
For some reason, an extra :00 is added at the end...

#5 KhAoZ

KhAoZ
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 05 July 2006 - 05:44 PM

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?

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 05 July 2006 - 06:08 PM

Why don't you use the PHP functions date() and strtotime() that are designed to handle dates?

<?php
$date = '2006-07-03 01:02:26';
echo date('F j, Y g:i a', strtotime($date));
?>

Ken

#7 KhAoZ

KhAoZ
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 06 July 2006 - 04:19 PM

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...

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 July 2006 - 04:51 PM

I just tried your original code and got the correct answer using both echo and return.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users