Symfony根据日期计算天数

时间:2017-08-31 09:37:34

标签: php mysql symfony calculator

我正在尝试根据日期计算1-21天的天数价格。

的HomeController

$Sql = ' SELECT DISTINCT 
            a.property_id, a.date, a.minimum_stay,
            a.maximum_stay,a.quantity,
            a.arrival_allowed,a.departure_allowed,
            p.duration, p.persons, p.amount, 
            p.extra_person_price, p.minimum_stay AS price_minimum_stay, 
            p.maximum_stay AS price_maximum_stay, p.weekdays, 
            p.period_till, p.period_from,
            datediff(p.period_till, p.period_from) AS number_of_days
            FROM availabilities AS a 
            JOIN prices AS p 
            ON a.property_id=p.property_id 
            WHERE a.minimum_stay >0 
            AND a.maximum_stay < 22 
            AND a.date >= p.period_from 
            AND a.date <= p.period_till
     ';
        $Stm = $this->getEntityManager()->getConnection()->prepare($Sql);
        $Stm->execute();
        return $Stm->fetchAll(PDOConnection::FETCH_ASSOC);




    public function CalculatePrice($persons, $extra_person_price, $date, $amount, $duration, $period_till, $period_from)
    {
        //loop through persons
        foreach ($persons as $person) {
           //calculate price for persons
            if ($person > 1) {
                $amount += $person * $extra_person_price;
            }
            //array to link parameters with database fields
            $tmp = array(
                "date" => $date,
                "person" => $person,
                "price_person" => number_format($amount / 100, 2, '.', ',')
            );
            //loop through $tmp an add value from 2 to 21 to day an add this to $tmp array with calculated value
            //$x=$number_of_days;
            //$days = (strtotime($period_till) - strtotime($period_from)) / (60 * 60 * 24);

    for ($x = 1; $x <= 21; ++$x) {
                if ($x >1) {
                    $tmp["day$x"] = $tmp["day".($x-1)] + number_format($amount  / 100, 2, '.', ',');
                    //number_format(($amount * $x) / 100, 2, '.', ',');
                } else {
                    $tmp["day$x"] = "0.00";
                }

            $price_per_person[] = $tmp;
        }

        return $price_per_person;
    }

我也在计算人数的价格,但那部分是好的。现在我已经制作了一个for循环并存储了从1到21的数字,在function CalculatePrice中查看我的第二个for循环。但这部分不好我需要根据日期来计算。例如: 大多数日子的定期价格是123欧元。比如9月3日那天花费250欧元,9月7日花费300欧元。所以,请说一个人想要停留5天,他到9月3日,所以计算将是:250 + 123 + 123 + 123 + 300 = 919欧元。 但我需要这个基于日期。我试过了period_from和period_til,但到目前为止还没有运气。有人可以提供一个示例或一些有用的提示我如何在for循环中执行此操作,就像我的第二个for循环一样。

2 个答案:

答案 0 :(得分:0)

我已根据理论T. Abdelmalek给了我这个问题。我制作了一个数组,并存储了每个日期的日期和价格。答案已被删除,我不知道如何标记为已解决

答案 1 :(得分:0)

您可以像这样使用date_diff

First date = $leave->getLeaveFrom();

    Second date = $leave->getLeaveTo();
    $diff = date_diff($leave->getLeaveFrom(),$leave->getLeaveTo());

Results = var_dump($diff);
相关问题