<?php
$tz = new DateTimeZone("UTC");
//lets say he chosed the service on Monday and Tuesday and Wednesday
$days = "Monday ,Tuesday, Wednesday";// <-- change to user input
$planDays = translateWeekDaysToNumbers($days);
$planName = "Weekly";// <- change as user input
if($planName == 'Weekly'){
$weekOrMonth = "week";
} else if ($planName == 'Monthly'){
$weekOrMonth = "month";
} else {
//TODO: throw error
}
$startString = "2019-02-01 00:00:00"; // <- change later to "now"
$startDate = new DateTime($startString, $tz);
$afterMonth = new DateTime("@" . strtotime("+1 $weekOrMonth", $startDate->format("U")));
$numberOfDays = $afterMonth->diff($startDate)->days;
$daysOfService = 0; // what we need to calculate
for($i = 0; $i<$numberOfDays; $i++){
$dateInterval = new DateInterval("P{$i}D");
$startDate->add($dateInterval);
if (in_array($startDate->format("w"), $planDays)) $daysOfService ++;
$startDate->sub($dateInterval);//reset to first day
}
$pro_price = 100;// <--- change to your price from the DB
$totalPrice = $daysOfService * $pro_price;
echo $daysOfService . "\n";
echo $totalPrice . "\n";
exit;
function translateWeekDaysToNumbers(string $days)
{
//dont change the numbers as they are the given by ->format("w")
$daysNumbers = [
"sunday" => 0,
"monday" => 1,
"tuesday" => 2,
"wednesday" => 3,
"thursday" => 4,
"friday" => 5,
"saturday" => 6
];
$days = explode(",", $days);
$planDays = [];
foreach($days as $day){
$day = strtolower(trim($day));
if (isset($daysNumbers[$day])) $planDays[] = $daysNumbers[$day];
}
return $planDays;
}
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
3
300