Jump to content

[SOLVED] Problem returning vars from function


Democreous

Recommended Posts

My vars will not return from the function :

 

function timecor($time)
{

list($hour, $min, $sec) = explode(":", $time); // splits $time apart into $hour $min $sec

if ($hour > 12) 
	{
	$hour=($hour-12);
	$ampm = "pm";
	}
else ($ampm = "am");

$hour=($hour/1); // get rid of 0

return $hour; 
return $min;
return $sec;
return $ampm;
}

 

When I call it with

 


timecor($e[event_time]); // Call Function where event_time is a var equal to HH:MM:SS

 

Is this because the vars I am returning never existed until I created them in the Function?  Any help would be appreciated.

Link to comment
Share on other sites

When you return a value from a function you need to assign the returned value to a variable. Just calling the function doesn't assign the returned value to anything.

 

<?php
$somevar = timecor($e[event_time]);
?>

 

Also, you shouldn't use parenthesis in this statement:

<?php
$timetotal = ($hour.":".$min." ".$ampm);
return $timetotal;
?>

 

This can be written as

<?php
return ($hour.":".$min." ".$ampm);
?>

 

BTW, what you are attempting to do here can probably be done with a combination of the strtotime() function and the date() function.

 

Ken

Link to comment
Share on other sites

Try echoing  your vars inside your function to see if they are correct.

I've never seen this before:

list($hour, $min, $sec) = explode(":", $time)';

...pretty clever if it works.

 

Anyhow, why are you going thru all this trouble in the first place?

try :

date('l:i p',strtotime($time));

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.