Welcome Guest, Not a member yet? Register   Sign In
How to calculate a product price hourly and days price?
#1

I create a bike rental project and I am confusing in hourly and day wise rent calculation

product_filter.php(view)
------------------------------------------------------------------------------------------------------------------------------------------------------
<?php include('header.php'); ?>
    <?php
        $city = $this->input->get('city');
        $startdate = $this->input->get('startdate');
        $enddate = $this->input->get('enddate');
        $bikename = $this->input->get('bikename');
        //$days = $this->input->get('Days');
    $total_days = 0;
    $GLOBALS['total_days'] = 0;
    $total_hours = 0;
    $GLOBALS['total_hours'] = 0;   
    ?>
    <!--<div style="background: url('<?php echo base_url('assets/img/about-top.png'); ?>')no-repeat scroll center center / cover; padding-bottom: 125px; padding-top:125px;"></div>-->
          <div class="container" style="margin-top: 30px;">
                   
                   
                    <?php echo form_open('user/filter', 'method="get"'); ?>
                    <div class="row search-form home_filter">
                   
                   
                    <?php echo form_dropdown(['name'=>'city','class'=>'col form-control','options'=>['Select City'=>'Select City','Delhi'=>'Delhi','Mumbai'=>'Mumbai','Pune'=>'Pune','Agra'=>'Agra','Ahmedabad'=>'Ahmedabad','Mysuru'=>'Mysuru'],'selected'=>$city]); ?>
                   
                   
                    <?php echo form_dropdown(['name'=>'daytime','class'=>'col form-control','options'=>['Hourly/Daily'=>'Hourly/Daily','30 Day Booking'=>'30 Day Booking']]); ?>
                   
                    <?php echo form_input(['name'=>'startdate','class'=>'col form-control','id'=>'start','value'=>$startdate]); ?>
                    <span class="input-group-addon"><i class="fa fa-calendar" aria-hidden="true"></i></span>
                    <!--<input class="col-md-2 form-control" type="text" id="start" value="Start Date" style="float: left;" />-->
                    <!--<input class="col-md-2 form-control" type="datetime-local" value="End Date" style="float: left;" />-->
                    <?php echo form_input(['name'=>'enddate','class'=>'col form-control','id'=>'enddate','value'=>$enddate]); ?>
                    <span class="input-group-addon"><i class="fa fa-calendar" aria-hidden="true"></i></span>
                    <input class="col form-control" type="submit" name="submit" value="RIDE NOW" style="float: left;" />
                   
                    </div>
                    </form>
                    </div> 
       
        <div class="container">
           
       
            <div class="row" style="margin-top: 50px;">
                <div class="col-md-3">
                <div class="products"style="text-align: left; padding-left:10px; padding-right:10px;">
                <h3>Filter</h3>
                <!--<form name="filter">-->
                <?php echo form_open('user/filter', 'method="get"'); ?>
               
                <?php
                //$city = $this->input->get('city');
                echo form_hidden('city',$city); ?>
                <h4>Select Date &amp; Time</h4>
                <label><strong>Start Date</strong></label>
                <?php echo form_input(['name'=>'startdate','class'=>'form-control','id'=>'startside','value'=>$startdate]); ?>
                <br />
                <label><strong>Select End Date</strong></label>
                <?php echo form_input(['name'=>'enddate','class'=>'form-control','id'=>'enddateside','value'=>$enddate]); ?>
                <?php /* ?>
                <?php echo form_checkbox(['name'=>'Delhi','value'=>'Delhi','style'=>'height:25px; width:auto;float:left;margin-right:5px;']) . "<p style='margin-bottom:0px'>Delhi</p>"; ?>
                <?php echo form_checkbox(['name'=>'Mumbai','value'=>'Mumbai','style'=>'height:25px; width:auto;float:left;margin-right:5px;']) . "<p style='margin-bottom:0px'>Mumbai</p>"; ?>
                <?php echo form_checkbox(['name'=>'Agar','value'=>'Agra','style'=>'height:25px; width:auto;float:left;margin-right:5px;']) . "<p style='margin-bottom:0px'>Agra</p>"; ?>
                <?php echo form_checkbox(['name'=>'Pune','value'=>'Pune','style'=>'height:25px; width:auto;float:left;margin-right:5px;']) . "<p style='margin-bottom:0px'>Pune</p>"; ?>
                <?php echo form_checkbox(['name'=>'Goa','value'=>'Goa','style'=>'height:25px; width:auto;float:left;margin-right:5px;']) . "<p style='margin-bottom:0px'>Goa</p>"; ?>
                <?php */ ?>
               
                <h4 style="margin-top: 30px;">Search Duration</h4>
                <?php
                  /* $origin = new DateTime($startdate);
                    $target = new DateTime($enddate);
                    $interval = $origin->diff($target);
                    echo $interval->format('%R%a days');*/
                 
                 
                 
                  echo return_date_diff_string($startdate,$enddate,$total_days,$total_hours);
                   
                   
                    function return_date_diff_string($startdate,$enddate,$total_days,$total_hours){
                        $datetime1 = new DateTime($startdate);
                        $datetime2 = new DateTime($enddate);
                        $difference = $datetime1->diff($datetime2);
                        $final_str='';
                       
                        $y = $difference->y;
                        $m = $difference->m;
                        $d = $difference->d;
                        $h = $difference->h;
                        $min = $difference->i;
                       
                        $GLOBALS['total_days'] = $d;
                        $total_days = $d;
                        $GLOBALS['total_hours'] = $h;
                        $total_hours = $h;                       
                       
                       
                        if($y>0){
                            $final_str.=$y.' years<input type="hidden" name="years" id="years" value="'.$y.'" />';
                        }else{
                            echo '<input type="hidden" name="years" id="years" value="'.$y.'" />';
                        }
                       
                        if($m>0){
                            if(!empty($final_str)){$final_str.=', ';}
                            $final_str.=$m.' months<input type="hidden" name="months" id="months" value="'.$m.'" />';
                        }else{
                            echo '<input type="hidden" name="months" id="months" value="'.$m.'" />';
                        }
                       
                        if($d>0){
                            if(!empty($final_str)){$final_str.=', ';}
                            $final_str.=$d.' Days<input type="hidden" name="Days" id="Days" value="'.$d.'" />';
                        }else{
                            echo '<input type="hidden" name="Days" id="Days" value="'.$d.'" />';
                        }
                       
                        if($h>0){
                            if(!empty($final_str)){$final_str.=', ';}
                            $final_str.=$h.' Hr<input type="hidden" name="Hr" id="Hr" value="'.$h.'" />';
                        }else{
                            echo '<input type="hidden" name="Hr" id="Hr" value="'.$h.'" />';
                        }
                       
                        if($min>0){
                            if(!empty($final_str)){$final_str.=', ';}
                            $final_str.=$min.' Min<input type="hidden" name="Min" id="Min" value="'.$min.'" />';
                        }else{
                            echo '<input type="hidden" name="Min" id="Min" value="'.$min.'" />';
                        }
                       
                        return $final_str;
                    }
                    //echo $startdate;
                   
                   
                    /*$start = new DateTime("08-03-2017 09:26:00");
                      $end = new DateTime("11-03-2017 09:25:00");
                      $interval = $start->diff($end);
                      $hrs = $interval->d * 24 + $interval->h;
                      echo $hrs." hours ".$interval->format('%i')." minutes"; */
                   
                 
                ?>
               
                <h4 style="margin-top:20px; margin-bottom:20px;">Search by Bike Model</h4>
                <?php /* ?><?php echo form_checkbox(['name'=>'bikename','value'=>'Royal Enfield Classic 350','style'=>'height:25px; width:auto;float:left;margin-right:5px;']) . "<p style='margin-bottom:0px'>Royal Enfield Classic 350</p>"; ?>
                <?php echo form_checkbox(['name'=>'bikename','value'=>'Honda Navi','style'=>'height:25px; width:auto;float:left;margin-right:5px;']) . "<p style='margin-bottom:0px'>Honda Navi</p>"; ?>
                <?php echo form_checkbox(['name'=>'bikename','value'=>'Suzuki Swish','style'=>'height:25px; width:auto;float:left;margin-right:5px;']) . "<p style='margin-bottom:0px'>Suzuki Swish</p>"; ?>
                <?php echo form_checkbox(['name'=>'bikename','value'=>'TVS Apache 160 4V','style'=>'height:25px; width:auto;float:left;margin-right:5px;']) . "<p style='margin-bottom:0px'>TVS Apache 160 4V</p>"; ?>
                <?php */ ?>
                <?php if(count($data)): ?>
                <?php foreach($data as $viewdata): ?>
                <?php echo form_checkbox(['name'=>'bikename','value'=>$viewdata->pro_name,'style'=>'height:25px; width:auto;float:left;margin-right:5px;']) . "<p style='margin-bottom:0px'>" . "$viewdata->pro_name" . "</p>"; ?>
               
                <?php  //echo $viewdata->area; ?>
                <?php endforeach; ?>
                    <?php else: ?>
                        <p>No records found.</p>
                    <?php endif; ?>
               
               
                <h4 style="margin-top:20px; margin-bottom:20px;">Search by Location</h4>
               
                <?php if(count($data)): ?>
                <?php foreach($data as $viewdata): ?>
                <?php echo form_checkbox(['name'=>'location','value'=>$viewdata->area,'style'=>'height:25px; width:auto;float:left;margin-right:5px;']) . "<p style='margin-bottom:0px'>" . "$viewdata->area" . "</p>"; ?>
               
                <?php  //echo $viewdata->area; ?>
                <?php endforeach; ?>
                    <?php else: ?>
                        <p>No records found.</p>
                    <?php endif; ?>
               
                <?php echo form_submit(['name'=>'submit','value'=>'Apply Filter', 'class'=>'btnapplyfilter']); ?>
                </form>
                <script>
                    var startdatetime = new Date("<?php echo date("D M d Y G:iConfused"); ?> +0530");
                    startdatetime.setDate(startdatetime.getDate());
                        $('#start').datetimepicker({
                          format:"Y-m-d H:i",
                          minDate: startdatetime,
                          validateOnBlur: false,
                          step:30
                        });
                        $('#enddate').datetimepicker({
                          format:"Y-m-d H:i",
                          minDate: startdatetime,
                          validateOnBlur: false,
                          step:30
                        });
                       
                       
                        var startdatetime = new Date("<?php echo date("D M d Y G:iConfused"); ?> +0530");
                        startdatetime.setDate(startdatetime.getDate());
                            $('#startside').datetimepicker({
                              format:"Y-m-d H:i",
                              minDate: startdatetime,
                              validateOnBlur: false,
                              step:30
                            });
                            $('#enddateside').datetimepicker({
                              format:"Y-m-d H:i",
                              minDate: startdatetime,
                              validateOnBlur: false,
                              step:30
                            });
               
               
               
                </script>
               
                </div>
               
               
                </div>
                <div class="col-md-9">
                    <div class="row">
                 
                   
                    <?php if(count($data)): ?>
                    <?php foreach($data as $viewdata): ?>
                        <div class="col-md-4" style="margin-bottom: 30px;">
                        <div class="products">
                        <div style="float: right; background: #fed809; padding: 10px; border-radius:3px;color:#000;padding:5px 15px 5px 15px; margin-bottom: 30px;">5 Left</div>
                        <img src="<?php  echo $viewdata->image_path; ?>" style="margin-bottom: 20px;" />
                        <h4 style="font-size: 17px;"><?php  echo $viewdata->pro_name; ?></h4>
                        <p><?php  echo $viewdata->pro_desc; ?></p>
                        <div class="row" style="margin-top: 20px;">
                            <div class="col-md-6"><strong style="color:red;font-size:20px;">₹
                            <?php 
                                //echo $viewdata->pro_rent;
                           
                                $a = $viewdata->pro_rent/3;
                                $b = $a/100*30;
                                $weeklyprice = round($a+$b);
                                $firstdayprice = $weeklyprice/5;
                                $fourteenthdayprice = $weeklyprice + $a*1.3;
                            /*echo $GLOBALS['total_days'];*/
                            /*if($GLOBALS['total_hours']<=6)
                            {
                                $sixhoursprice = $firstdayprice/4;
                                echo round($sixhoursprice,0);
                            }
                            elseif($GLOBALS['total_hours']<=12)
                            {
                                $twelvehoursprice = $firstdayprice/3;
                                echo round($twelvehoursprice,0);
                            }
                            elseif($GLOBALS['total_hours']<=18)
                            {
                                $eighteenhoursprice = $firstdayprice/2;
                                echo round($eighteenhoursprice,0);
                            }
                            elseif($GLOBALS['total_hours']<=24)
                            {
                                $twentyhoursprice = $firstdayprice/1;
                                echo round($twentyhoursprice,0);
                            }*/
                            if($GLOBALS['total_days']==1)
                            {
                                //$firstdayprice = $weeklyprice/5;
                                echo round($firstdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==2)
                            {
                                $seconddayprice = $firstdayprice*2;
                                echo round($seconddayprice,0);
                            }
                            elseif($GLOBALS['total_days']==3)
                            {
                                $thirddayprice = $firstdayprice*3;
                                echo round($thirddayprice,0);
                            }
                            elseif($GLOBALS['total_days']==4)
                            {
                                $forthdayprice = $firstdayprice*4;
                                echo round($forthdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==5)
                            {
                                $fifthdayprice = $a + $a/100*15;
                                echo round($fifthdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==6)
                            {
                                $sixthdayprice = $a/5*6;
                                echo round($sixthdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==7)
                            {
                                $sevenhdayprice = $weeklyprice;
                                echo round($sevenhdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==8)
                            {
                                $eighthdayprice = $weeklyprice + $a*1.2/5*1;
                                echo round($eighthdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==9)
                            {
                                $ninthdayprice = $weeklyprice + $a*1.2/5*2;
                                echo round($ninthdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==10)
                            {
                                $tenthdayprice = $weeklyprice + $a*1.2/5*3;
                                echo round($tenthdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==11)
                            {
                                $eleventhdayprice = $weeklyprice + $a*1.2/5*4;
                                echo round($eleventhdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==12)
                            {
                                $twelfthdayprice = $weeklyprice + $a*1.1/5*5;
                                echo round($twelfthdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==13)
                            {
                                $thirteendayprice = $weeklyprice + $a/5*6;
                                echo round($thirteendayprice,0);
                            }
                            elseif($GLOBALS['total_days']==14)
                            {
                               
                                echo round($fourteenthdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==15)
                            {
                                $fifteenthdayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*1;
                                echo round($fifteenthdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==16)
                            {
                                $sixteenthdayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*2;
                                echo round($sixteenthdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==17)
                            {
                                $seventeenthdayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*3;
                                echo round($seventeenthdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==18)
                            {
                                $eighteenthdayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*4;
                                echo round($eighteenthdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==19)
                            {
                                $ninteenthdayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*5;
                                echo round($ninteenthdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==20)
                            {
                                $twentydayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*6;
                                echo round($twentydayprice,0);
                            }
                            elseif($GLOBALS['total_days']==21)
                            {
                                $twentyonedayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*7;
                                echo round($twentyonedayprice,0);
                            }
                            elseif($GLOBALS['total_days']==22)
                            {
                                $twentytwodayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*8;
                                echo round($twentytwodayprice,0);
                            }
                            elseif($GLOBALS['total_days']==23)
                            {
                                $twentythreedayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*9;
                                echo round($twentythreedayprice,0);
                            }
                            elseif($GLOBALS['total_days']==24)
                            {
                                $twentyfourdayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*10;
                                echo round($twentyfourdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==25)
                            {
                                $twentyfivedayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*11;
                                echo round($twentyfivedayprice,0);
                            }
                            elseif($GLOBALS['total_days']==26)
                            {
                                $twentysixdayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*12;
                                echo round($twentysixdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==27)
                            {
                                $twentysevendayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*13;
                                echo round($twentysevendayprice,0);
                            }
                            elseif($GLOBALS['total_days']==28)
                            {
                                $twentyeightdayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*14;
                                echo round($twentyeightdayprice,0);
                            }
                            elseif($GLOBALS['total_days']==29)
                            {
                                $twentyninedayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*15;
                                echo round($twentyninedayprice,0);
                            }
                            elseif($GLOBALS['total_days']==30)
                            {
                                $thirtydayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*16;
                                echo round($thirtydayprice,0);
                            }
                            elseif($GLOBALS['total_days']==31)
                            {
                                $thirtyonedayprice = $fourteenthdayprice + ($viewdata->pro_rent - $fourteenthdayprice)/16*17;
                                echo round($thirtyonedayprice,0);
                            }
                            else
                            {
                                echo $viewdata->pro_rent;
                            }
                            ?></strong></div>
                            <div class="col-md-6"><a href="#" style="background: #fed809; padding: 10px; border-radius:3px;color:#000;font-weight:600;font-size: 15px;">Book Now</a></div>
                        </div>
                        </div>
                        </div>
                    <?php endforeach; ?>
                    <?php else: ?>
                        <p>No records found.</p>
                    <?php endif; ?>
                    </div>
                   
                   
                </div>
            </div>
        </div>           

<style>.xdsoft_datetimepicker .xdsoft_calendar td:hover, .xdsoft_datetimepicker .xdsoft_timepicker .xdsoft_time_box>div>div:hover{background:#000!important;}</style>
<?php include('footer.php'); ?>

--------------------------------------------------------------------------------------------------------------------------------------------------------

(controller)
-----------------------
public function filter()
        {
            $city = $this->input->get('city');
            $bikename = $this->input->get('bikename');
            $location = $this->input->get('location');
           
            if(!empty($city))
            {
            $this->load->model('product');
            $prodata = $this->product->product_filter($city,$bikename,$location);
           
            $this->load->view('product_filter',['data'=>$prodata]);
            } else{
           
            $this->load->model('product');
            $prodata = $this->product->view_product();
           
            $this->load->view('product_filter',['data'=>$prodata]);
            }
        }


----------------------------------------------------------------------------------------------------------------------------------
(Model)
-----------------------------------------------------
public function view_product()
        {
            $qry = $this->db->select()->from('products')->get();
            return $qry->result();
        }
       
        public function product_filter($city,$bikename,$location)
        {
           
            if(!empty($bikename)){
                   
                $qry = $this->db->select()->from('products')->where(['city'=>$city,'pro_name'=>$bikename])->get();
                return $qry->result();
               
            }
            elseif(!empty($location)){
                $qry = $this->db->select()->from('products')->where(['city'=>$city,'area'=>$location])->get();
                return $qry->result();
            }
            else{
                   
                $qry = $this->db->select()->from('products')->where('city',$city)->get();
                return $qry->result();
               
            }
           
           
        }
Reply
#2

(This post was last modified: 05-09-2021, 04:56 AM by kleber.)

Hi, you have put a lot of code and most of it has nothing to do with the question. But it's ok.

I would convert any unit of time (hour and day) to timestamp and use the smallest unit (hour) to calculate the price.

For example:

https://schoolsofweb.com/how-to-calculat...es-in-php/

PHP Code:
$date1 "2021-05-27 01:00:00";

$date2 "2021-05-28 02:00:00";

$timestamp1 strtotime($date1);

$timestamp2 strtotime($date2);

$hour_diff abs($timestamp2 $timestamp1)/(60*60);

$price_hour 2; 

$final_price 
hour_diff*price_hour
Reply




Theme © iAndrew 2016 - Forum software by © MyBB