3v4l.org

run code in 300+ PHP versions simultaneously
<?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; }
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
3 300
Output for 8.3.5
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

preferences:
166.52 ms | 402 KiB | 225 Q