php计算器函数变量和时间

时间:2011-11-06 20:09:35

标签: php

您好我正在尝试创建一个基本上这样做的PHP计算器:

$price_m_Fuengirola = 11.00;

/* number of people */

$car1 = 0.00; /* 1-4 */
$car2 = 50.00; /* 4-8 */
$car3 = 70.00; /* 8-12 */
$car4 = 90.00; /* 12-16 */

/* Calculator */

date_default_timezone_set('GMT');

//echo $newTime;

echo $price_m_Fuengirola."<br />";

echo $car2."<br />";

function timeCalc(){
$newTime = date("H:i", time());

if ($newTime < "18:56"){

    echo 20.00;
    //echo "<br />this is more time";

} else {

    echo 6.00;
    //echo "this is more time";

}
}


$timeCalc = timeCalc();

echo $timeCalc."<br />";

$total = $price_m_Fuengirola + $car2 + $timeCalc."<br /><br />";

echo $total;`

背后的逻辑是,如果一个人在x时间之前订购出租车,那么他们会收取费用x否则他们会收取费用x(我计划在我设法让它工作时更改它),问题是价格正在增加到汽车价格而不是功能价格输出。在函数中,我试图只回显一个不是时间的数字,所以我错过了什么?

1 个答案:

答案 0 :(得分:1)

以下是我的特别看法:

<?php

function rateOffset($now, $cutoff){
    if ($now < strtotime($cutoff)){
        return 20.00;
    } else {
        return 6.00;
    }
}

$cutoff = "18:56";

for ($i = 0; $i < 24; $i++) {
    $now = strtotime("$i:00");
    $rateCalc = rateOffset($now, $cutoff);
    echo date('H:i', $now)." = $rateCalc\n";
}

?>

http://ideone.com/owfLg

注意,我正在比较两个参数$now(这是一个时间戳)和$cutoff,它是要检查的截止时间的hh:mm表示。 for循环用于演示每小时的结果,即:

00:00 = 20
01:00 = 20
02:00 = 20
03:00 = 20
04:00 = 20
05:00 = 20
06:00 = 20
07:00 = 20
08:00 = 20
09:00 = 20
10:00 = 20
11:00 = 20
12:00 = 20
13:00 = 20
14:00 = 20
15:00 = 20
16:00 = 20
17:00 = 20
18:00 = 20
19:00 = 6
20:00 = 6
21:00 = 6
22:00 = 6
23:00 = 6