PHP的回声功能不打印结果

时间:2018-08-26 22:46:03

标签: php function

我有一个脚本来计算每天的票务开放时间,但只计算我们开放的时间。功能的细节没有那么重要,但我将其全部粘贴在这里,因为这可能是失败的原因。

此功能:

function getTime($o, $now, $total) {

    //One Day in seconds
    $oneDay = 86400;

    //One Business day (12 hours, 7am-7pm)
    $oneBDay = 43200;

    //Get the timestamp of 7am/7pm for time given

    $sevenAM = mktime('7', '0', '0', m($o), d($o), y($o));
    $sevenPM = mktime('19', '0', '0', m($o), d($o), y($o));

    //If Ticket Start Time is before 7am, we only count from 7am after
    if ($o < $sevenAM) {
        $o = $sevenAM;
    } else {
        $o = $o;
    }

    //Debug to get today
    $today = date('Y-m-d h:i:s a', $o);

    //See if we are within the same business day
    $diff = $now - $o;

    //Debug
    //echo $today.",".$timeSpent.",".$total."\n";

    //If we are not within 1 business day, do this again
    if ($diff > $oneBDay) {

        //Total Time spent for the day
        $timeSpent = $sevenPM - $o;

        //Add todays time to total time
        $total = $total + $timeSpent;

        //Move to tomorrow
        $o = $sevenAM + $oneDay;

        getTime($o, $now, $total);
    }

    //If we are within 1 business day, count the time for today and return our result
    if ($diff < $oneBDay) {
        $time = $diff;
        $total = $total + $time; //for example $total = 123456
                    return $total;
            }
}

当我这样做

echo getTime($o,$now,0); 

我希望看到123456。但是我什么也没打印。

该函数运行,我知道total具有值(我已将其静态设置为调试)。

-请注意该函数在需要时会自行调用

其他功能:

function y($o){

    $y = date('Y',$o);
    return $y;
}
function m($o){
    $m = date('m',$o);
    return $m;
}
function d($o){
    $d = date('d',$o);
    return $d;
}

编辑:

如果我这样做:

        if ($diff < $oneBDay) {
        $time = $diff;
        $total = $total + $time; //for example $total = 123456
        echo "My Total:".$total;
                    return $total;
            }

我将看到我的总数:123456

2 个答案:

答案 0 :(得分:2)

指出结构缺陷很难调试您的功能并非徒劳。我建议您为功能创建一个单元测试,然后进行重构,以便能够确保保留所需的行为。

也就是说,您的函数正在打印任何内容,因为它没有达到任何显式的return指令。因此它返回null。如果没有找到最后一个if,您将错过最后一次返回值的机会。

我不确定您的函数应该做什么,但是如果您递归调用它,您可能想返回它的值,或者至少要重新分配给一个变量。结帐。

<?php

function y($o){ $y = date('Y',$o); return $y; }
function m($o){ $m = date('m',$o); return $m; }
function d($o){ $d = date('d',$o); return $d; }


function getTime($o,$now,$total){

//One Day in seconds
$oneDay = 86400;

//One Business day (12 hours, 7am-7pm)
$oneBDay = 43200;

//Get the timestamp of 7am/7pm for time given

    $sevenAM = mktime('7','0','0',m($o),d($o),y($o));
    $sevenPM = mktime('19','0','0',m($o),d($o),y($o));

//If Ticket Start Time is before 7am, we only count from 7am after
    if ($o < $sevenAM){
            $o = $sevenAM;
    }else{
            $o = $o;
    }

//Debug to get today
    $today = date('Y-m-d h:i:s a',$o);

//See if we are within the same business day
    $diff = $now - $o;

//Debug
    //echo $today.",".$timeSpent.",".$total."\n";

//If we are not within 1 business day, do this again
            if ($diff > $oneBDay){

                    //Total Time spent for the day
                    $timeSpent = $sevenPM - $o;

                    //Add todays time to total time
                    $total = $total+$timeSpent;

                    //Move to tomorrow
                    $o = $sevenAM+$oneDay;

                    return getTime($o,$now,$total); // LOOKS LIKE YOU WANT TO RETURN VALUE HERE
            }

//If we are within 1 business day, count the time for today and return our result
            if($diff < $oneBDay){
                    $time = $diff;
                    $total = $total+$time; // FIXED MISSING SEMICOLON HERE TO AVOID SYNTAX ERROR
                    return $total;
            }
 }


$test = getTime(1534964212, date('U'), 0);

echo "$test"; // 144885

?>

答案 1 :(得分:1)

尽管我不确定您要使用此功能做什么,但我已纠正了一些语法错误,并为缺少的情况添加了一些返回。 (在if ($diff > $oneBDay) {内部并将if ($diff < $oneBDay) {更改为if ($diff <= $oneBDay) {

<?php

    function getTime($o, $now, $total) {

        //One Day in seconds
        $oneDay = 86400;

        //One Business day (12 hours, 7am-7pm)
        $oneBDay = 43200;

        //Get the timestamp of 7am/7pm for time given

        $sevenAM = mktime('7', '0', '0', m($o), d($o), y($o));
        $sevenPM = mktime('19', '0', '0', m($o), d($o), y($o));

        //If Ticket Start Time is before 7am, we only count from 7am after
        if ($o < $sevenAM) {
            $o = $sevenAM;
        }

        //See if we are within the same business day
        $diff = $now - $o;

        //If we are not within 1 business day, do this again
        if ($diff > $oneBDay) {

            //Total Time spent for the day
            $timeSpent = $sevenPM - $o;

            //Add todays time to total time
            $total = $total + $timeSpent;

            //Move to tomorrow
            $o = $sevenAM + $oneDay;

            return getTime($o, $now, $total);
        }

        //If we are within 1 business day, count the time for today and return our result
        if ($diff <= $oneBDay) {
            $time = $diff;
            $total = $total + $time; //for example $total = 123456;
            return $total;
        }
    }

    function y($o){ $y = date('Y',$o); return $y; }

    function m($o){ $y = date('m',$o); return $y; }

    function d($o){ $y = date('d',$o); return $y; }

    $o = 1534964212;
    $now = date('U');

    echo getTime($o,$now,0);

现在它返回类似145111

的内容