【问题标题】:Symfony calculate number of days based on dateSymfony 根据日期计算天数
【发布时间】:2017-08-31 09:37:34
【问题描述】:

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

家庭控制器

$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 循环。

【问题讨论】:

  • 您的最后一个 for 循环似乎假定了一个固定价格。我建议您手动设置 $tmp["day2"] 并在所有 >2 天执行 $tmp["day$x"] = $tmp["day".($x-1)] + $price_of_the_day; (输出时进行格式化)。然后想办法计算当天 $x 的价格(start_date + $x,使用日期函数)。
  • thanx,但是$price_of_the_day 是什么意思,你的意思是注释变量 $day 吗?
  • 我的意思是,$price_of_the_day 是当天(住宿)$date 加上$x 天的价格。当然这是你必须以某种方式计算的东西,但我认为这应该不难。
  • @T. Abdelmalek 我已经根据 T. Abdelmalek 给我的理论解决了这个问题。我制作了一个数组并存储了每个日期的日期和价格,然后遍历。答案已被删除,我不知道如何标记为已解决。
  • @T. Abdelmalek 你能帮忙解决这个问题吗here

标签: php mysql symfony calculator


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你可以这样使用date_diff

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 2012-02-03
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      相关资源
      最近更新 更多