Here's one way
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-03-12 16:12:00";
$time2 = "2022-03-12 18:31:00";
$instance = new PriceCalculator($time1, $time2);
echo $instance->calculate(); // 242.53