I have a php application to calculate table price by each minutes. I would like to migrate to Meteorjs, since function “reactive-var” is great to show at user-side and backend-side.
But how to convert below PHP PriceCalculator to JS? any hint or apps like this?
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],
'H' => [ 98, 128] // holiday prices
];
private $holidays = [
'2022-04-15',
'2022-04-18',
'2022-05-02'
];
public $periods = [];
public $total = 0;
public function __construct ($time1, $time2)
{
$this->start = new DateTime($time1);
$this->start->modify('-1 minute');
$this->end = new DateTime($time2);
}
public function calculate()
{
$total = 0;
$prevday = $prevpk = $prevh = null;
$inc = new DateInterval('PT1M');
$min = clone $this->start;
$min->modify("+1 minute");
while ($min < $this->end) {
$day = $min->format('w');
if (in_array($min->format('Y-m-d'), $this->holidays)) {
$day = 'H';
}
$peak = '02' <= $min->format('H') && $min->format('H') < '18' ? 0 : 1;
if ($prevday != $min->format('Y-m-d') || $prevpk != $peak) {
$prevday = $min->format('Y-m-d');
$prevpk = $peak;
$prevh = $min->format('d H');
}
$this->total += $this->price[$day][$prevpk]/60;
$min->add($inc);
}
}
}