Jump to content

kit123321

Members
  • Posts

    16
  • Joined

  • Last visited

Everything 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? 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. Thank you so much for your help, i am still learning your script. your help is very useful.
  3. i found that missing Holiday which is same calculation with Weekend cost. 2022-05-02 2022-05-09 2022-09-12 i am thinking what is the best way to add with this Holiday array.
  4. Thanks Barand for the reply. Let's me understand and study your script. give me a sec.
  5. Yeah, Barand. you are correct. sorry my mistake that i am talking row, not column. but i find that, when the period more than one day, the calc not correct. Pic1 - correct if only 1 day. Pic2 - incorrect if more than 1 day.
  6. 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>';
  7. like this right hand size output is what i want. $time1(Start time) & $time2(End time) are variables, so the $rentals array should be automatically value from $time1,2.
  8. i still have no idea how to breakdown the date range into column automatically. any hint?
  9. 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
  10. Thanks for your reply Barand, pls ignore above information, my mistake. is it possible to add calculating summary in script?
  11. 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
  12. just tested, calc result not correct. the correct count for below time1,time2, should be = 588+128 = $716
  13. 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.