Jump to content

kit123321

Members
  • Posts

    16
  • Joined

  • Last visited

Posts posted by kit123321

  1. when i click button .btnStart, i would like to INPUT class="stime" to have value $resp['start'] without page reload. but not work. any help?

    2022-08-11_084035.thumb.jpg.dc7b103d3d7e3b8664799774b540baaa.jpg

    JS code

            $(".btnStart").click( function() {
                var tid = $(this).data("tid")
                $.get(
                    "",
                    {"ajax":"startBooking", "tid":tid},
                    function(resp) {
                        $(".sdate[data-tid="+tid+"]").val(resp.sdate).show()
                        $(".stime[data-tid="+tid+"]").val(resp.start).show()
                        $(".bid[data-tid="+tid+"]").val(resp.bid)
                        
                    },
                    "JSON"
                )
                $(this).hide()
                $(".btnFinish[data-tid="+tid+"]").show()
    
            })

    PHP Handle AJAX

    ##   Handle AJAX requests  
        if ($_GET['ajax'] == 'startBooking') {
            $dt = new DateTime('now');
            $start = $dt->format('Y-m-d H:i:s');
            $st = $dt->format('H:i');
            $sd = $dt->format('Y-m-d');
            $stmt = $pdo->prepare("INSERT INTO table_booking (table_id, start_time) VALUES (?, ?)");
            $stmt->execute( [ $_GET['tid'], $start ] );
            $bid = $pdo->lastInsertId();
            $resp = [ "bid" => $bid,
                      "start" => $st,
                      "sdate" => $sd
                    ];
            exit( json_encode($resp));
        }
    

    HTML

    <input type='time' class='stime' value='$st' data-tid='{$r['tid']}'>

     

  2. 6 minutes ago, mac_gyver said:

    please post the actual code and output in the forum, using the <> menu button. pictures are impossible to use as input for testing.

    i am sorry about that, since i would like you to check the output.

     

    <?php
    
    class PriceCalculator
    {
        private $start;
        private $end;
        private $price = [
                          0 => [ 98, 128],
                          1 => [ 88, 118],
                          2 => [ 88, 118],
                          3 => [ 88, 118],
                          4 => [ 88, 118],
                          5 => [ 88, 118],
                          6 => [ 98, 128]
                         ];
        
        public function __construct ($time1, $time2)
        {
            $this->start = new DateTime($time1);
            $this->end   = new DateTime($time2);
        }
        
        public function calculate()
        {
            $total = 0;
            $dp = new DatePeriod($this->start, new DateInterval('PT1M'), $this->end );
            foreach ($dp as $min) {
                $day = $min->format('w');
                $peak = '02' <= $min->format('H') && $min->format('H') < '18' ? 0 : 1;
    			$total += $this->price[$day][$peak]/60;
            }
    
            return number_format($total, 2);
        }
    }
    
    $time1 = "2022-04-11 16:05:00";
    $time2 = "2022-04-11 18:12:00";
    
    $instance = new PriceCalculator($time1, $time2);
    echo $instance->calculate();                                 // 242.53
    
    $rentals = [
                 [ "2022-04-11 16:05:00", "2022-04-11 18:00:00" ],
                 [ "2022-04-11 18:00:00", "2022-04-11 18:12:00" ]
               ];
    echo '<pre>';
    foreach ($rentals as $times) {
        $instance = new PriceCalculator($times[0], $times[1]);
        printf('%s | %s | %10s<br>', $times[0], $times[1], $instance->calculate());
    }
    echo '</pre>';

     

  3. Yes, you correct that my example calculations mistake, thanks for correction.

    and Yes, that's what i need for the cost breakdown, but $time1 and $time2 are variable.

    for example.

    $time1 = "2022-04-11 16:05:00";
    $time2 = "2022-04-11 18:12:00";

     

    $rentals = [
                 [ "2022-04-11 16:05:00", "2022-04-11 18:00:00" ],
                 [ "2022-04-11 18:00:00", "2022-04-11 18:12:00" ]
               ];
    echo '<pre>';
    foreach ($rentals as $times) {
        $instance = new PriceCalculator($times[0], $times[1]);
        printf('%s | %s | %10s<br>', $times[0], $times[1], $instance->calculate());
    }
    echo '</pre>';

     

    is it possible that the output automatically like this:
     

    192.27
    2022-04-11 16:05:00 | 2022-04-11 18:00:00 |     168.67
    2022-04-11 18:00:00 | 2022-04-11 18:12:00 |      23.60

     

  4. imagine that you go rental a table. from Friday 12:00 to Saturday 12:00 = total 24 hours.

    so should be = $88(weekday 1200-1800) *6 + $118(weekday 1800-0000) *6 + $128(weekend 0000-0200) *2+ $98(weekend 0200-1800) *10

    = $2,472

    time different price calc as below.

    $week_day = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
    $weekend_day = ['Sat', 'Sun'];
    $price_weekday_hour = 88;       // weekday 0200-1800
    $price_weekday_peakhour = 118;  // weekday 1800-0200
    $price_weekend_hour = 98;       // weekend 0200-1800
    $price_weekend_peakhour = 128;  // weekend 1800-0200

     

  5. My objective.

    $timestamp1 = rental start time, $timestamp2 rental end time, in order to calc price.
    Price calc based on weekday and weekend timerange.
    Example: start: Fri 1700 end: Fri 1900 = 88+118 = $206
    I don't know how to calc the duration within different timerange, to use different price cost by hour.

    $week_day = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
    $weekend_day = ['Sat', 'Sun'];
    $price_weekday_hour = 88;       // weekday 0200-1800
    $price_weekday_peakhour = 118;  // weekday 1800-0200
    $price_weekend_hour = 98;       // weekend 0200-1800
    $price_weekend_peakhour = 128;  // weekend 1800-0200

     

    <?php
    
    class priceCalculator
    {
        public $start;
    
        public $end;
    
        public function __construct($timestamp1, $timestamp2)
        {
            $this->start = $timestamp1;
            $this->end = $timestamp2;
        }
    
        public function calculate()
        {
            if ($this->isPeakHour() && $this->isWeekend()){
                // calculate
            }
        }
    
        /**
         * true = peak
         * false = normal
         *
         * @return bool
         */
        private function isPeakHour()
        {
            $startdateH = intval(date("H", $this->start));
            $enddateH = intval(date("H", $this->end));
    
            if ($startdateH > 18 && $enddateH < 2){
                return true;
            }
            return false;
        }
    
        /**
         * true = weekend
         * false = weekday
         *
         * @return bool
         */
        private function isWeekend()
        {
            $weekDay = date('w', $this->end);
            return ($weekDay == 0 || $weekDay == 6);
        }
    }
    
    $timestamp1 = strtotime("2022-03-12 16:12:00");
    $timestamp2 = strtotime("2022-03-12 18:31:00");
    
    $instance = new PriceCalculator($timestamp1, $timestamp2);
    $instance->calculate();
    
    ?>

     

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.