1

我现在有点卡住了,感谢一些帮助。我有一个带有 2 列“月”和“价格”的 Mysql 表,即 1 月 >> 5(欧元);二月>> 7(欧元)等等。在网站的预订表格上,用户可以选择到达日期和离开日期。如果用户选择一个介于其间的日期,例如 20.01.2013-12.04.2013,如何计算其间的完整月份(02 和 03)?

这是我必须计算到达和离开月份的天数:

 ...
 $query="SELECT * FROM pricecal";
 $result=mysql_query($query);
 $num=mysql_numrows($result);

 $i=0;

 while ($i < $num)
  {

$month=mysql_result($result,$i,"month");
$price=mysql_result($result,$i,"price");

$i++;

$a = $_POST['dropToDay'];
$b = $_POST['dropToMonth'];
$c = $_POST['dropToYear'];
$d = $_POST['dropOffDay'];
$e = $_POST['dropOffMonth'];
$f = $_POST['dropOffYear'];

$days_to = (date('t',mktime(0, 0, 0, $a,(date($b) +1), date($c))) - $a +1);
$off_month_dif = (date('d-m-Y',mktime(0, 0, 0, $d,(date($e)), date($f))) - $d);
$days_off = (date('d-m-Y',mktime(0, 0, 0, $d,(date($e)), date($f))) - $off_month_dif);
$days = (date($f.$e.$d) - date($c.$b.$a));
$days_all = ($days+1);

if($b==$e AND $b==$month)
 {
  $b = $month;
  $pricing = $days_all*$price;
  echo 'Price for '.$days_all.' days is '.$pricing.' EUR.';
 }

if($b == $month AND $b != $e)
 {
  $pricing1 = $days_to*$price;
 }
if($e == $month AND $b != $e)
 {
  $pricing = $days_off*$price;
  $pricings_all = $pricing+$pricing1;
  echo 'Price for booking time: '.$pricings_all.' EUR';
 }
 .....
4

3 回答 3

0

只是大声假设。这是你想要的?这条路有什么?

SQLFIDDLE MYSQL 演示

select months, price
from demo, (select @i:=-1) r
where months = 
  date_format(
    Date_Add(Date '2013.01.13',
             interval @i:=@i+1 month),'%b')
having @i <= period_diff(date_format
(Date '2013-04-12', '%Y%m'),
date_format(Date '2013-01-20', '%Y%m'))
;

| MONTHS | PRICE |
------------------
|    Jan |     5 |
|    Feb |     7 |
|    Mar |     9 |
|    Apr |    10 |
于 2013-02-02T18:41:37.717 回答
0

构建一个月份和价格数组(我假设您存储的是数字月份):

$price = array();
$query = "SELECT month, price FROM pricecal";
$result = mysql_query($query) or die(mysql_error()); 
while($row = mysql_fetch_assoc($result)) {
    $price[$result["month"]] = $result["price"];
}
// array(
// 1 => 5,
// 2 => 7,
// ...
// )

接下来,构建两个时间戳,遍历它们并求和:

$ts1 = mktime(0, 0, 0, $_POST['dropToMonth'],  $_POST['dropToDay'],  $_POST['dropToYear']);
$ts2 = mktime(0, 0, 0, $_POST['dropOffMonth'], $_POST['dropOffDay'], $_POST['dropOffYear']);

for($ts = $ts1, $sum = 0; $ts <= $ts2; $ts = strtotime("+1 day", $ts)) {
    $sum += $price[date("n", $ts)];
}
于 2013-02-02T21:28:40.950 回答
0

这将使您在单个查询中获得两个日期之间的总费用。最里面的子查询看起来有点吓人,但它只是为范围之间的每一天创建一行。然后按月分组。

演示: http ://sqlfiddle.com/#!2/53b3b/1

输出:

447

询问:

SET @beginDate = '2013-01-20';
SET @endDate   = '2013-04-12';

SELECT
    SUM( days.days * rate.price ) AS fee

FROM
    rate

    INNER JOIN
    ( SELECT 
        MONTH( a.Date ) AS month,
        COUNT( * )      AS days

    FROM (
        select @endDate - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
        from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
        cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
        cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
        ) a

    WHERE 
        a.Date BETWEEN @beginDate AND @endDate 

    GROUP BY
        MONTH( a.Date )

    ) AS days
    ON rate.month = days.month

架构:

CREATE TABLE 
    rate 
    (
    month INT, 
    price INT
    );

INSERT INTO 
    rate
    ( 
    month, 
    price
    )

VALUES
    ( 1, 5 ),
    ( 2, 7 ),
    ( 3, 5 ),
    ( 4, 3 );
于 2013-02-02T21:59:42.233 回答