[英]php echo function not printing result
我有一個腳本來計算每天門票打開的時間,但只有我們開放的幾個小時。 功能的細節並不那么重要,但我已將其全部粘貼在這里,因為它可能是失敗的原因。
這里的功能:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.