时间计算功能,PHP

时间:2015-01-28 11:06:15

标签: php wordpress

任何人都可以告诉我为什么这不起作用? 第一个kode snippert只有if就像魅力一样,如下面的帖子和一个来自mysql数据库索引的数组在while循环中

<?php
$time = $_POST['modt'];
$min = 0;

if (strpos($time, ':') !== false) { 

list ($hr, $min) = explode(':',$time);

$time = (((int)$hr) * 60) + (((int)$min));

}


echo $time;
echo $min;
?> 

但是当我把它写成一个函数时它只是不工作.....

<?php
$time = $_POST['modt'];
$min = 0;

function timecalc($time, $min) {
if (strpos($time, ':') !== false) { 

list ($hr, $min) = explode(':',$time);

$time = (((int)$hr) * 60) + (((int)$min));

}
return $time;
}

echo $time;
echo $min;
?>

所以...我可以说是一个监督某事的白痴。 提前谢谢!为了你的帮助:))

3 个答案:

答案 0 :(得分:3)

您甚至没有调用该功能。 您可以返回一个数组,而不是像下面那样回显这些值:

<?php
$time = $_POST['modt'];
$min = 0;

function timecalc($time, $min) {
if (strpos($time, ':') !== false) { 

list ($hr, $min) = explode(':',$time);

$time = (((int)$hr) * 60) + (((int)$min));

}
$return = array();
$return['time'] = $time;
$return['min'] = $min;
return $return;
}

$returnFromFunction = timecalc($time, $min);
echo $returnFromFunction['time'];
echo $returnFromFunction['min'];
?>

答案 1 :(得分:0)

<?php
$time = $_POST['modt'];
$min = 0;

function timecalc() {
    global $time;
    global $min;
    if (strpos($time, ':') !== false) { 

        list ($hr, $min) = explode(':',$time);

        $time = (((int)$hr) * 60) + (((int)$min));

    }
    return $time;
}

echo $time;
echo $min;
?>

答案 2 :(得分:0)

<?php
$time = $_POST['modt'];
$min = 0;

function timecalc($time, $min) {
if (strpos($time, ':') !== false) { 

list ($hr, $min) = explode(':',$time);

$time = (((int)$hr) * 60) + (((int)$min));

}
return $time;
}
 $timeData= timecalc($time, $min); // Note: you need to call the function
echo $time;
echo $min;

?>