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; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 48, Position 2 = 51
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 59
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
2 jumps found. (Code = 77) Position 1 = 69, Position 2 = 119
Branch analysis from position: 69
2 jumps found. (Code = 78) Position 1 = 70, Position 2 = 119
Branch analysis from position: 70
2 jumps found. (Code = 43) Position 1 = 77, Position 2 = 84
Branch analysis from position: 77
1 jumps found. (Code = 42) Position 1 = 85
Branch analysis from position: 85
2 jumps found. (Code = 46) Position 1 = 88, Position 2 = 93
Branch analysis from position: 88
2 jumps found. (Code = 47) Position 1 = 90, Position 2 = 92
Branch analysis from position: 90
2 jumps found. (Code = 43) Position 1 = 94, Position 2 = 116
Branch analysis from position: 94
2 jumps found. (Code = 43) Position 1 = 96, Position 2 = 98
Branch analysis from position: 96
1 jumps found. (Code = 42) Position 1 = 99
Branch analysis from position: 99
2 jumps found. (Code = 46) Position 1 = 101, Position 2 = 103
Branch analysis from position: 101
2 jumps found. (Code = 43) Position 1 = 104, Position 2 = 106
Branch analysis from position: 104
1 jumps found. (Code = 42) Position 1 = 107
Branch analysis from position: 107
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
Branch analysis from position: 106
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
Branch analysis from position: 103
Branch analysis from position: 98
2 jumps found. (Code = 46) Position 1 = 101, Position 2 = 103
Branch analysis from position: 101
Branch analysis from position: 103
Branch analysis from position: 116
Branch analysis from position: 92
Branch analysis from position: 93
Branch analysis from position: 84
2 jumps found. (Code = 46) Position 1 = 88, Position 2 = 93
Branch analysis from position: 88
Branch analysis from position: 93
Branch analysis from position: 119
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 119
Branch analysis from position: 59
2 jumps found. (Code = 77) Position 1 = 69, Position 2 = 119
Branch analysis from position: 69
Branch analysis from position: 119
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 59
Branch analysis from position: 56
Branch analysis from position: 59
filename:       /in/rnYYA
function name:  (null)
number of ops:  137
compiled vars:  !0 = $bookings, !1 = $input, !2 = $year, !3 = $month, !4 = $start_date, !5 = $end_date, !6 = $test_period, !7 = $total_capacity, !8 = $booked_capacity, !9 = $total_revenue, !10 = $booking, !11 = $booking_start_date, !12 = $booking_end_date, !13 = $booking_overlap_start, !14 = $booking_overlap_end, !15 = $period, !16 = $unreserved_capacity
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   INIT_FCALL                                               'array_map'
          1        SEND_VAL                                                 'str_getcsv'
          2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '%0A'
          4        INIT_FCALL_BY_NAME                                       'getCSV'
          5        DO_FCALL                                      0  $17     
          6        SEND_VAR                                                 $17
          7        DO_ICALL                                         $18     
          8        SEND_VAR                                                 $18
          9        DO_ICALL                                         $19     
         10        ASSIGN                                                   !0, $19
    5    11        INIT_FCALL                                               'array_shift'
         12        SEND_REF                                                 !0
         13        DO_ICALL                                                 
    8    14        INIT_FCALL                                               'explode'
         15        SEND_VAL                                                 '-'
         16        INIT_FCALL                                               'trim'
         17        INIT_FCALL                                               'fgets'
         18        INIT_FCALL                                               'fopen'
         19        SEND_VAL                                                 'php%3A%2F%2Fstdin'
         20        SEND_VAL                                                 'r'
         21        DO_ICALL                                         $22     
         22        SEND_VAR                                                 $22
         23        DO_ICALL                                         $23     
         24        SEND_VAR                                                 $23
         25        DO_ICALL                                         $24     
         26        SEND_VAR                                                 $24
         27        DO_ICALL                                         $25     
         28        ASSIGN                                                   !1, $25
    9    29        FETCH_DIM_R                                      ~27     !1, 0
         30        ASSIGN                                                   !2, ~27
   10    31        FETCH_DIM_R                                      ~29     !1, 1
         32        ASSIGN                                                   !3, ~29
   13    33        INIT_FCALL                                               'mktime'
         34        SEND_VAL                                                 0
         35        SEND_VAL                                                 0
         36        SEND_VAL                                                 0
         37        SEND_VAR                                                 !3
         38        SEND_VAL                                                 1
         39        SEND_VAR                                                 !2
         40        DO_ICALL                                         $31     
         41        ASSIGN                                                   !4, $31
   14    42        INIT_FCALL                                               'mktime'
         43        SEND_VAL                                                 0
         44        SEND_VAL                                                 0
         45        SEND_VAL                                                 0
         46        IS_SMALLER                                               !3, 12
         47      > JMPZ                                                     ~33, ->51
         48    >   ADD                                              ~34     !3, 1
         49        QM_ASSIGN                                        ~35     ~34
         50      > JMP                                                      ->52
         51    >   QM_ASSIGN                                        ~35     1
         52    >   SEND_VAL                                                 ~35
         53        SEND_VAL                                                 1
         54        IS_EQUAL                                                 !3, 12
         55      > JMPZ                                                     ~36, ->59
         56    >   ADD                                              ~37     !2, 1
         57        QM_ASSIGN                                        ~38     ~37
         58      > JMP                                                      ->60
         59    >   QM_ASSIGN                                        ~38     !2
         60    >   SEND_VAL                                                 ~38
         61        DO_ICALL                                         $39     
         62        ASSIGN                                                   !5, $39
   17    63        SUB                                              ~41     !5, !4
         64        ASSIGN                                                   !6, ~41
   19    65        ASSIGN                                                   !7, 0
   20    66        ASSIGN                                                   !8, 0
   21    67        ASSIGN                                                   !9, 0
   22    68      > FE_RESET_R                                       $46     !0, ->119
         69    > > FE_FETCH_R                                               $46, !10, ->119
   23    70    >   INIT_FCALL                                               'strtotime'
         71        FETCH_DIM_R                                      ~47     !10, 2
         72        SEND_VAL                                                 ~47
         73        DO_ICALL                                         $48     
         74        ASSIGN                                                   !11, $48
   24    75        FETCH_DIM_R                                      ~50     !10, 3
         76      > JMPZ                                                     ~50, ->84
         77    >   INIT_FCALL                                               'strtotime'
         78        FETCH_DIM_R                                      ~51     !10, 3
         79        SEND_VAL                                                 ~51
         80        DO_ICALL                                         $52     
         81        ADD                                              ~53     $52, 86400
         82        QM_ASSIGN                                        ~54     ~53
         83      > JMP                                                      ->85
         84    >   QM_ASSIGN                                        ~54     <false>
         85    >   ASSIGN                                                   !12, ~54
   27    86        IS_SMALLER                                       ~56     !11, !5
         87      > JMPZ_EX                                          ~56     ~56, ->93
   28    88    >   BOOL_NOT                                         ~57     !12
         89      > JMPNZ_EX                                         ~57     ~57, ->92
         90    >   IS_SMALLER                                       ~58     !4, !12
         91        BOOL                                             ~57     ~58
         92    >   BOOL                                             ~56     ~57
         93    > > JMPZ                                                     ~56, ->116
   31    94    >   IS_SMALLER                                               !4, !11
         95      > JMPZ                                                     ~59, ->98
         96    >   QM_ASSIGN                                        ~60     !11
         97      > JMP                                                      ->99
         98    >   QM_ASSIGN                                        ~60     !4
         99    >   ASSIGN                                                   !13, ~60
   32   100      > JMPZ_EX                                          ~62     !12, ->103
        101    >   IS_SMALLER                                       ~63     !12, !5
        102        BOOL                                             ~62     ~63
        103    > > JMPZ                                                     ~62, ->106
        104    >   QM_ASSIGN                                        ~64     !12
        105      > JMP                                                      ->107
        106    >   QM_ASSIGN                                        ~64     !5
        107    >   ASSIGN                                                   !14, ~64
   35   108        SUB                                              ~66     !14, !13
        109        ASSIGN                                                   !15, ~66
   38   110        FETCH_DIM_R                                      ~68     !10, 1
        111        MUL                                              ~69     !15, ~68
        112        DIV                                              ~70     ~69, !6
        113        ASSIGN_OP                                     1          !9, ~70
   39   114        FETCH_DIM_R                                      ~72     !10, 0
        115        ASSIGN_OP                                     1          !8, ~72
   42   116    >   FETCH_DIM_R                                      ~74     !10, 0
        117        ASSIGN_OP                                     1          !7, ~74
   22   118      > JMP                                                      ->69
        119    >   FE_FREE                                                  $46
   45   120        SUB                                              ~76     !7, !8
        121        ASSIGN                                                   !16, ~76
   47   122        INIT_FCALL                                               'setlocale'
        123        SEND_VAL                                                 4
        124        SEND_VAL                                                 'en_US'
        125        DO_ICALL                                                 
   48   126        INIT_FCALL_BY_NAME                                       'money_format'
        127        SEND_VAL_EX                                              '%25.2n'
        128        SEND_VAR_EX                                              !9
        129        DO_FCALL                                      0  $79     
        130        CONCAT                                           ~80     'expected+revenue%3A+', $79
        131        ROPE_INIT                                     3  ~82     '%2C+expected+total+capacity+of+the+unreserved+offices%3A+'
        132        ROPE_ADD                                      1  ~82     ~82, !16
        133        ROPE_END                                      2  ~81     ~82, '%0A'
        134        CONCAT                                           ~84     ~80, ~81
        135        ECHO                                                     ~84
  128   136      > RETURN                                                   1

Function getcsv:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rnYYA
function name:  getCSV
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E > > RETURN                                                   'Capacity%2CMonthlyPrice%2CStartDay%2CEndDay%0A1%2C600%2C2014-07-01%2C%0A1%2C400%2C2014-04-02%2C%0A1%2C400%2C2014-05-01%2C%0A5%2C2800%2C2014-03-01%2C2014-04-30%0A2%2C1500%2C2014-05-01%2C2014-06-30%0A4%2C1700%2C2014-04-01%2C%0A3%2C1300%2C2014-04-01%2C%0A15%2C6500%2C2014-05-01%2C2014-08-31%0A1%2C400%2C2014-05-01%2C%0A1%2C400%2C2014-05-01%2C%0A3%2C1400%2C2014-05-01%2C%0A18%2C7200%2C2014-05-01%2C%0A1%2C800%2C2014-06-01%2C%0A1%2C700%2C2014-05-01%2C2014-06-30%0A2%2C1250%2C2014-04-16%2C2014-06-02%0A1%2C600%2C2013-11-01%2C2014-05-31%0A8%2C4000%2C2014-06-02%2C2014-07-31%0A2%2C1300%2C2014-05-01%2C2014-10-31%0A4%2C2200%2C2014-05-01%2C%0A14%2C11875%2C2014-06-01%2C%0A2%2C1500%2C2014-05-01%2C2014-08-31%0A2%2C1500%2C2012-06-01%2C%0A3%2C1850%2C2014-04-09%2C2014-08-06%0A2%2C1100%2C2014-05-01%2C2014-09-30%0A1%2C625%2C2014-04-11%2C%0A1%2C1000%2C2014-02-14%2C%0A1%2C400%2C2014-05-01%2C%0A6%2C3600%2C2014-04-02%2C%0A2%2C950%2C2013-02-01%2C2014-05-31%0A4%2C2500%2C2013-06-01%2C2014-04-30%0A2%2C1200%2C2014-07-01%2C2014-08-31%0A1%2C950%2C2014-06-01%2C2014-08-31%0A4%2C3200%2C2014-04-01%2C2014-09-30%0A1%2C400%2C2014-03-27%2C2014-04-10%0A4%2C2600%2C2014-02-01%2C2014-04-08%0A11%2C5500%2C2014-05-01%2C%0A2%2C1200%2C2014-05-01%2C2014-07-02%0A1%2C600%2C2014-05-01%2C2014-07-31%0A1%2C800%2C2013-11-01%2C2014-04-30%0A1%2C700%2C2013-05-01%2C%0A2%2C900%2C2014-07-01%2C2014-08-31%0A2%2C1400%2C2014-04-02%2C%0A2%2C1500%2C2014-05-01%2C%0A2%2C1200%2C2014-05-01%2C%0A2%2C1500%2C2014-05-01%2C2014-10-31%0A2%2C900%2C2014-02-01%2C%0A1%2C400%2C2014-04-14%2C%0A2%2C900%2C2014-01-01%2C2014-06-30%0A2%2C1000%2C2013-12-01%2C2014-06-30%0A9%2C4500%2C2014-02-06%2C2014-04-30%0A4%2C2500%2C2013-08-01%2C%0A6%2C2900%2C2013-05-01%2C2014-05-31%0A1%2C600%2C2014-04-09%2C%0A2%2C1700%2C2014-02-14%2C2014-04-30%0A2%2C900%2C2014-07-01%2C%0A1%2C400%2C2014-05-01%2C2014-10-31%0A2%2C0%2C2014-04-15%2C2014-05-01%0A4%2C2500%2C2014-05-01%2C%0A4%2C3600%2C2014-05-01%2C%0A9%2C4950%2C2014-05-01%2C%0A2%2C1100%2C2014-05-12%2C%0A6%2C2700%2C2014-05-01%2C2014-08-31%0A16%2C350%2C2014-04-01%2C2014-08-31%0A2%2C1300%2C2012-10-01%2C%0A1%2C950%2C2014-06-01%2C2014-06-08%0A3%2C2000%2C2014-06-01%2C2014-06-30%0A8%2C3600%2C2014-07-08%2C%0A6%2C5400%2C2014-05-12%2C%0A4%2C2700%2C2013-09-01%2C%0A4%2C2600%2C2012-06-01%2C2014-05-31%0A4%2C2700%2C2012-07-01%2C2014-04-30%0A1%2C450%2C2014-04-02%2C%0A4%2C2700%2C2014-04-01%2C2014-04-30%0A4%2C2200%2C2014-06-01%2C2014-10-31'
  128     1*     > RETURN                                                   null

End of function getcsv

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.15 ms | 1412 KiB | 31 Q