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.

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

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));

Archived

This topic is now archived and is closed to further replies.

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