dongyoufo5672 2018-08-26 22:46
浏览 60
已采纳

php echo函数不打印结果

I have a script to count the time a ticket is open each day, but only for hours we are open. The details of the functions arent that important but i have pasted it all here as it may be the cause for the failure.

Heres the 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."
";

    //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;
            }
}

when I do

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

I would expect to see 123456. But i get nothing printed.

The function runs and I know total has a value ( I have set it statically as a debug).

--Note the function calls itself if needed

additional functions:

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

EDIT:

if i do :

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

I will see My Total:123456

  • 写回答

2条回答 默认 最新

  • doushouj966020 2018-08-26 23:17
    关注

    It's not in vain to point out that your function is hard to debug due to structural flaws. I suggest you to create a Unit Test for your function and then refactor so you will be able to make sure that you preserve the desired behaviour.

    That said, your function is printing anything because it's not reaching any explicit return directive. So it returns null. See that if the last if is not hit, you will miss the last chance for returning a value.

    I'm not sure what your function should do, but it looks like if you call it recursively, you may want to return it's value, or at least reassign to a variable. Checkout for that.

    <?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."
    ";
    
    //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条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗