Jump to content

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


Guest KhAoZ

Recommended Posts

Guest KhAoZ
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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.