php echo函数不打印结果

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (210)

我有一个脚本来计算每天开票的时间,但只有几个小时我们才开门。功能的细节不是很重要,但我把它粘贴在这里,因为它可能是失败的原因。

继承人的功能:

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

提问于
用户回答回答于

由于结构缺陷,指出您的功能难以调试并非徒劳无功。我建议你为你的函数创建一个单元测试然后重构,这样你就能确保保留所需的行为。

也就是说,你的功能是打印任何东西,因为它没有达到任何明确的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

?>
用户回答回答于

虽然我不确定你试图用这个函数做什么,但我已经纠正了一些语法错误,并为缺少的案例添加了一些回复。(内部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

扫码关注云+社区

领取腾讯云代金券