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; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
2 jumps found. (Code = 44) Position 1 = 71, Position 2 = 46
Branch analysis from position: 71
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 64, Position 2 = 65
Branch analysis from position: 64
2 jumps found. (Code = 44) Position 1 = 71, Position 2 = 46
Branch analysis from position: 71
Branch analysis from position: 46
Branch analysis from position: 65
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 18
filename:       /in/IVFF7
function name:  (null)
number of ops:  80
compiled vars:  !0 = $tz, !1 = $days, !2 = $planDays, !3 = $planName, !4 = $weekOrMonth, !5 = $startString, !6 = $startDate, !7 = $afterMonth, !8 = $numberOfDays, !9 = $daysOfService, !10 = $i, !11 = $dateInterval, !12 = $pro_price, !13 = $totalPrice
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   NEW                                              $14     'DateTimeZone'
          1        SEND_VAL_EX                                              'UTC'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $14
    4     4        ASSIGN                                                   !1, 'Monday+%2CTuesday%2C+Wednesday'
    5     5        INIT_FCALL_BY_NAME                                       'translateWeekDaysToNumbers'
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0  $18     
          8        ASSIGN                                                   !2, $18
    6     9        ASSIGN                                                   !3, 'Weekly'
    8    10        IS_EQUAL                                                 !3, 'Weekly'
         11      > JMPZ                                                     ~21, ->14
    9    12    >   ASSIGN                                                   !4, 'week'
         13      > JMP                                                      ->18
   10    14    >   IS_EQUAL                                                 !3, 'Monthly'
         15      > JMPZ                                                     ~23, ->18
   11    16    >   ASSIGN                                                   !4, 'month'
         17      > JMP                                                      ->18
   16    18    >   ASSIGN                                                   !5, '2019-02-01+00%3A00%3A00'
   17    19        NEW                                              $26     'DateTime'
         20        SEND_VAR_EX                                              !5
         21        SEND_VAR_EX                                              !0
         22        DO_FCALL                                      0          
         23        ASSIGN                                                   !6, $26
   18    24        NEW                                              $29     'DateTime'
         25        INIT_FCALL                                               'strtotime'
         26        NOP                                                      
         27        FAST_CONCAT                                      ~30     '%2B1+', !4
         28        SEND_VAL                                                 ~30
         29        INIT_METHOD_CALL                                         !6, 'format'
         30        SEND_VAL_EX                                              'U'
         31        DO_FCALL                                      0  $31     
         32        SEND_VAR                                                 $31
         33        DO_ICALL                                         $32     
         34        CONCAT                                           ~33     '%40', $32
         35        SEND_VAL_EX                                              ~33
         36        DO_FCALL                                      0          
         37        ASSIGN                                                   !7, $29
   20    38        INIT_METHOD_CALL                                         !7, 'diff'
         39        SEND_VAR_EX                                              !6
         40        DO_FCALL                                      0  $36     
         41        FETCH_OBJ_R                                      ~37     $36, 'days'
         42        ASSIGN                                                   !8, ~37
   23    43        ASSIGN                                                   !9, 0
   25    44        ASSIGN                                                   !10, 0
         45      > JMP                                                      ->69
   26    46    >   NEW                                              $41     'DateInterval'
         47        ROPE_INIT                                     3  ~43     'P'
         48        ROPE_ADD                                      1  ~43     ~43, !10
         49        ROPE_END                                      2  ~42     ~43, 'D'
         50        SEND_VAL_EX                                              ~42
         51        DO_FCALL                                      0          
         52        ASSIGN                                                   !11, $41
   27    53        INIT_METHOD_CALL                                         !6, 'add'
         54        SEND_VAR_EX                                              !11
         55        DO_FCALL                                      0          
   28    56        INIT_FCALL                                               'in_array'
         57        INIT_METHOD_CALL                                         !6, 'format'
         58        SEND_VAL_EX                                              'w'
         59        DO_FCALL                                      0  $48     
         60        SEND_VAR                                                 $48
         61        SEND_VAR                                                 !2
         62        DO_ICALL                                         $49     
         63      > JMPZ                                                     $49, ->65
         64    >   PRE_INC                                                  !9
   29    65    >   INIT_METHOD_CALL                                         !6, 'sub'
         66        SEND_VAR_EX                                              !11
         67        DO_FCALL                                      0          
   25    68        PRE_INC                                                  !10
         69    >   IS_SMALLER                                               !10, !8
         70      > JMPNZ                                                    ~53, ->46
   32    71    >   ASSIGN                                                   !12, 100
   33    72        MUL                                              ~55     !9, !12
         73        ASSIGN                                                   !13, ~55
   34    74        CONCAT                                           ~57     !9, '%0A'
         75        ECHO                                                     ~57
   35    76        CONCAT                                           ~58     !13, '%0A'
         77        ECHO                                                     ~58
   36    78      > EXIT                                                     
   59    79*     > RETURN                                                   1

Function translateweekdaystonumbers:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 23
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 23
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 22
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 22
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/IVFF7
function name:  translateWeekDaysToNumbers
number of ops:  26
compiled vars:  !0 = $days, !1 = $daysNumbers, !2 = $planDays, !3 = $day
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
   42     1        ASSIGN                                                   !1, <array>
   52     2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '%2C'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $5      
          6        ASSIGN                                                   !0, $5
   53     7        ASSIGN                                                   !2, <array>
   54     8      > FE_RESET_R                                       $8      !0, ->23
          9    > > FE_FETCH_R                                               $8, !3, ->23
   55    10    >   INIT_FCALL                                               'strtolower'
         11        INIT_FCALL                                               'trim'
         12        SEND_VAR                                                 !3
         13        DO_ICALL                                         $9      
         14        SEND_VAR                                                 $9
         15        DO_ICALL                                         $10     
         16        ASSIGN                                                   !3, $10
   56    17        ISSET_ISEMPTY_DIM_OBJ                         0          !1, !3
         18      > JMPZ                                                     ~12, ->22
         19    >   FETCH_DIM_R                                      ~14     !1, !3
         20        ASSIGN_DIM                                               !2
         21        OP_DATA                                                  ~14
   54    22    > > JMP                                                      ->9
         23    >   FE_FREE                                                  $8
   58    24      > RETURN                                                   !2
   59    25*     > RETURN                                                   null

End of function translateweekdaystonumbers

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.99 ms | 1409 KiB | 23 Q