3v4l.org

run code in 300+ PHP versions simultaneously
<?php // would probably stream reading instead of ingesting as array in real life (due to potentially high number of entries) $bookings = array_map('str_getcsv', explode("\n", getCSV())); array_shift($bookings); // getting input $input = explode('-', trim(fgets(fopen('php://stdin', 'r')))); $year = $input[0]; $month = $input[1]; // determining time boundaries of the month $start_date = mktime(0, 0, 0, $month, 1, $year); // first of the month $end_date = mktime(0, 0, 0, $month < 12 ? $month + 1 : 1, 1, $month == 12 ? $year + 1 : $year); // next month's first of the month // length of the period, used for proration $test_period = $end_date - $start_date; $total_capacity = 0; $booked_capacity = 0; $total_revenue = 0; foreach ($bookings as $booking) { $booking_start_date = strtotime($booking[2]); $booking_end_date = $booking[3] ? strtotime($booking[3]) + 86400: FALSE; // end of day // booking is matched if it starts before month ends and either open-ended or ends after month starts if ($booking_start_date < $end_date && (!$booking_end_date || $booking_end_date > $start_date)) { // effective dates within a month to calculate booking value off of $booking_overlap_start = $booking_start_date > $start_date ? $booking_start_date : $start_date; $booking_overlap_end = $booking_end_date && $booking_end_date < $end_date ? $booking_end_date : $end_date; // effective time booked $period = $booking_overlap_end - $booking_overlap_start; // prorating the value based on booked period $total_revenue += $booking[1] * $period / $test_period; $booked_capacity += $booking[0]; } $total_capacity += $booking[0]; } $unreserved_capacity = $total_capacity - $booked_capacity; setlocale(LC_MONETARY, 'en_US'); echo "expected revenue: " . money_format('%.2n', $total_revenue) . ", expected total capacity of the unreserved offices: $unreserved_capacity\n"; function getCSV () { return <<<END Capacity,MonthlyPrice,StartDay,EndDay 1,600,2014-07-01, 1,400,2014-04-02, 1,400,2014-05-01, 5,2800,2014-03-01,2014-04-30 2,1500,2014-05-01,2014-06-30 4,1700,2014-04-01, 3,1300,2014-04-01, 15,6500,2014-05-01,2014-08-31 1,400,2014-05-01, 1,400,2014-05-01, 3,1400,2014-05-01, 18,7200,2014-05-01, 1,800,2014-06-01, 1,700,2014-05-01,2014-06-30 2,1250,2014-04-16,2014-06-02 1,600,2013-11-01,2014-05-31 8,4000,2014-06-02,2014-07-31 2,1300,2014-05-01,2014-10-31 4,2200,2014-05-01, 14,11875,2014-06-01, 2,1500,2014-05-01,2014-08-31 2,1500,2012-06-01, 3,1850,2014-04-09,2014-08-06 2,1100,2014-05-01,2014-09-30 1,625,2014-04-11, 1,1000,2014-02-14, 1,400,2014-05-01, 6,3600,2014-04-02, 2,950,2013-02-01,2014-05-31 4,2500,2013-06-01,2014-04-30 2,1200,2014-07-01,2014-08-31 1,950,2014-06-01,2014-08-31 4,3200,2014-04-01,2014-09-30 1,400,2014-03-27,2014-04-10 4,2600,2014-02-01,2014-04-08 11,5500,2014-05-01, 2,1200,2014-05-01,2014-07-02 1,600,2014-05-01,2014-07-31 1,800,2013-11-01,2014-04-30 1,700,2013-05-01, 2,900,2014-07-01,2014-08-31 2,1400,2014-04-02, 2,1500,2014-05-01, 2,1200,2014-05-01, 2,1500,2014-05-01,2014-10-31 2,900,2014-02-01, 1,400,2014-04-14, 2,900,2014-01-01,2014-06-30 2,1000,2013-12-01,2014-06-30 9,4500,2014-02-06,2014-04-30 4,2500,2013-08-01, 6,2900,2013-05-01,2014-05-31 1,600,2014-04-09, 2,1700,2014-02-14,2014-04-30 2,900,2014-07-01, 1,400,2014-05-01,2014-10-31 2,0,2014-04-15,2014-05-01 4,2500,2014-05-01, 4,3600,2014-05-01, 9,4950,2014-05-01, 2,1100,2014-05-12, 6,2700,2014-05-01,2014-08-31 16,350,2014-04-01,2014-08-31 2,1300,2012-10-01, 1,950,2014-06-01,2014-06-08 3,2000,2014-06-01,2014-06-30 8,3600,2014-07-08, 6,5400,2014-05-12, 4,2700,2013-09-01, 4,2600,2012-06-01,2014-05-31 4,2700,2012-07-01,2014-04-30 1,450,2014-04-02, 4,2700,2014-04-01,2014-04-30 4,2200,2014-06-01,2014-10-31 END; }

preferences:
56.79 ms | 402 KiB | 5 Q