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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测