簡體   English   中英

PHP:3個時間段,價格不同

[英]PHP: 3 time periods with different pricing

全部都是關於預訂...

我有3個時間段:

 Period 3 from 01.01.2014 to 29.04.2014 - Price 3 (per day) Period 2 from 30.04.2014 to 14.06.2014 - Price 2 (per day) Period 1 from 15.06.2014 to 21.09.2014 - Price 1 (per day) Period 4 from 22.09.2014 to 29.04.2015 - Price 3 (per day) 

我已經進行了php計算,可以計算每個期間的預訂天數和定價。 但是我不知道如何在兩個期間之間進行計算。

例如:
有人26.01.2014 to 25.04.2014 = 89 days * Price 126.01.2014 to 25.04.2014 = 89 days * Price 1

但是,當有人從3期到1期進行預訂時,這真的變得很困難...我試圖分離計算:

   if ($check_in >= '2013-04-30' && $check_out <= '2014-09-21')
     {
    //Days and price calcs here
    // contains Period 2 and Period 1
     }

但是效果不好...

您有什么想法可以使整個計算完美地進行嗎?

我錯過了一些非常重要的事情。

結構如下:

期間1

if($numberDays == 1)
{
$price = $price1_period1
}

if($numberDays >= 2 && $numberDays <= 3)
{

$price = $price2_period1 * $numberDays;
}

if($numberDays >= 4 && $numberDays <= 6)
{
$price = $price3_period1 * $numberDays;
}

if($numberDays >= 7 && $numberDays <= 14)
{
$price = $price4_period1 * $numberDays;
}

if($numberDays >= 15 && $numberDays <= 29)
{

$price = $price5_period1 * $numberDays;
} 

if($numberDays >= 30)
{
$price = $price6_period1 * $numberDays;
}


其他時期也一樣。
例如:第2期的6天價格為$ price3_period2。

您可以每天生成一個價格。 然后從開始日期到結束日期循環,對天價求和以得出總價:

<?php
$oneDay = 24*3600;

$configs = array(
    array(
        'startTime' => strtotime('2014-01-01'),
        'endTime' => strtotime('2014-04-29'),
        'price' => 10
    ),
    array(
        'startTime' => strtotime('2014-04-30'),
        'endTime' => strtotime('2014-06-14'),
        'price' => 20
    ),
    array(
        'startTime' => strtotime('2014-06-15'),
        'endTime' => strtotime('2014-09-21'),
        'price' => 30
    ),
    array(
        'startTime' => strtotime('2014-09-22'),
        'endTime' => strtotime('2015-04-29'),
        'price' => 40
    ),
);

$prices = array();
foreach ($configs as $config)
{
    $time1 = $config['startTime'];
    $time2 = $config['endTime'];
    $price = $config['price'];

    while ($time1 <= $time2)
    {
        $prices[date('Y-m-d', $time1)] = $price;
        $time1 += $oneDay;
    }
}

/**
 * @param $checkIn in format YYYY-mm-dd
 * @param $checkOut in format YYYY-mm-dd
 */
function getTotalPrice($checkIn, $checkOut, $prices)
{
    $time1 = strtotime($checkIn);
    $time2 = strtotime($checkOut);

    $price = 0;
    while ($time1 <= $time2)
    {
        $time1 += 24 * 3600;
        $price += $prices[date('Y-m-d', $time1)];
    }

    return $price;
}

echo getTotalPrice('2014-01-04', '2014-01-09', $prices);

首先,我假設$check_in$check_out是從某種形式獲取的字符串,然后將它們與另一個字符串進行比較,它們中的任何一個都是日期。

您可以將$check_in$check_out都轉換為Datetime,然后進行比較,例如:

// Check In Date
$check_in_date = new Datetime($check_in);
$date_compare_in = new Datetime('2013-04-30');

$diff_in = $check_in_date->diff($date_compare_in);

// Check Out Date
$check_out_date = new Datetime($check_out);
$date_compare_out = new Datetime('2014-09-21');

$diff_out = $check_out_date->diff($date_compare_out);

現在$diff_in是一個DateInterval對象,您可以檢查天數,例如,如果小時數大於0,則$ check_in晚於比較日期,如果小於0,則$ check_in早於比較日期。

if($diff_in->h >= 0 and $diff_out->h <= 0){
// We are within this date range.
}

DateInterval對象具有以下結構:

DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 0
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 0
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM