doupinge9055 2013-02-02 17:56 采纳率: 100%
浏览 36
已采纳

根据设定价格和月份计算日期之间的价格[关闭]

I am kind of stuck now and appreciate some help please. I have a Mysql table with 2 columns "month" and "price" i.e. January >> 5 (Euro); February >> 7 (Euro) and so on. On the booking form of the website the user can select arrival date and departure date. If the user selects a date with months in between, say 20.01.2013-12.04.2013, how can one calculate the full months (02 and 03) in between?

This is what i have got to calculate the days in arrival and departure months:

 ...
 $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';
 }
 .....
  • 写回答

3条回答 默认 最新

  • doufangzhang4454 2013-02-02 21:28
    关注

    Build an array of month and prices (I am assuming that you are storing numeric months):

    $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,
    // ...
    // )
    

    Next, build the two timestamps, loop through them and sum:

    $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)];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据