kit123321
-
Posts
16 -
Joined
-
Last visited
Posts posted by kit123321
-
-
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']}'>
-
On 4/25/2022 at 6:02 PM, Barand said:
I would create an array of holiday dates. When checking for day of week, also check if the date is in the holiday array.
Thank you so much for your help, i am still learning your script.
your help is very useful.
-
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.
-
Thanks Barand for the reply. Let's me understand and study your script. give me a sec.
-
-
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>';
-
-
i still have no idea how to breakdown the date range into column automatically. any hint?
-
do you have any idea?
-
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
-
-
-
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
-
just tested, calc result not correct.
the correct count for below time1,time2, should be = 588+128 = $716
-
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(); ?>
When clicked, the value can not pass to input element without reload page.
in Javascript Help
Posted
issue fixed. please help to delete it