0

I want to display to a user an amount per day and increment it until the next 124 days assumed to be four months.

I have a system where a user invest and a total ernable profit is calculated with the percentage ROI for such stock. Assuming someone invested in a stockA that has 20% ROI and matures in 4 months; assuming a user purchased that stockA that is sold $100 and he bought 4 units meaning he spent $400 and will earn as follows:

$units = 4;
$cost = 100;
$roi = 20/100;
$total_invested = $units * $cost;

$profit  = $total_invest *  $roi;
// $profit will be $80

My problem is I want to display value of $profit/124 that is the displaying a fraction of total earning to the user daily until maturity of 4 months. I can't figure out how to do that daily not just with loop of 124 iterations.

that is if the user total earning is $80/124 giving 0.65 on the first day and increment it with same value the next day until it reached the end date which is 4 months from now

/**
 * 
 * display and increment earning every day for 4 months or 124 days
 * 
 */

function display_earning() {
  $profit = $profit_roi;
  $dateBegin = now();
  $dateEnd = "date of 4 month from now";
  $earning = 0;

  for ($i = 0; $i < 124; $i++) {
    $earning += $profit / 124;
    return $earning;
  }
}
Nick
  • 138,499
  • 22
  • 57
  • 95

2 Answers2

1

I think your problem is to update and save the updated earning of user for 4 months. To do that you need to write a server level cron-job that run on every day and a script in which you update and save the earning of user and check it has reached 4 months for that user since you started or not.

I hope you want this and that will help you. Happy Learning.

Moeez Saiyam
  • 107
  • 3
  • 11
  • thats where the challenge is, can you help me around the cron job stuff as i dont fully understand how to go about it – philipsniche Jun 03 '20 at 07:56
  • There are multiple tutorials of how to write cron jobs in php available. For a quick view you can check this: https://stackoverflow.com/questions/18737407/how-to-create-cron-job-using-php – Moeez Saiyam Jun 03 '20 at 09:09
0

It's not clear what you want your code to do. Do you want it to give the total earned cumulatively by day x or give the total earned on any one day? If it is any one day and you don't want to take compound interest into account the earning will be the same every single day. In which case all your function has to do is return

function return_earnings($profit, $totalDays) {
  return $profit / $totalDays;
}

as the amount earned each day will be the same as any other day. If you do want compound interest then you'll need to code that it. I assume you don't as there is no sign of it in the code you supplied.

I get the feeling what you are looking for instead is a list of how much was earned each day cumulatively. If that's the case, the code you've written is more appropriate but needs some modification. At the moment your code is returning $earning after the first iteration of the loop. Moving return to the end of the function should fix that.

function display_earning() {
  $profit = $profit_roi;
  $dateBegin = now();
  $dateEnd = "date of 4 month from now";
  $earning = 0;
  $totalDays = 124; // You might want to change '124' in future 
                    // and having $totalDays in the for loop makes it more
                    // obvious what your code is trying to do.

  for ($i = 0; $i < $totalDays; $i++) {
    $earning += $profit / $totalDays;
  }
  return $earning;
}

However now because of the line $earning += you will return the sum of earning at the end of $totalDays, ie the $profit, which you already know! You may want the cumulative earnings by a given day to be an item in an array, in which case:

function display_earning() {
  $profit = $profit_roi;
  $dateBegin = now();
  $dateEnd = "date of 4 month from now";
  $totalEarning = 0; //renamed to $totalEarning
  $earningsByDay = array();
  $totalDays = 124; 

  for ($i = 0; $i < $totalDays; $i++) {
    $totalEarning += $profit / $totalDays;
    $earningsByDay[$i] = $totalEarning;
  }
  return $earningsByDay;
}

This will now return an array with each element of the array amounting to the sum earned by that day. For example, $earningsByDay[0] will be the first day, $earningsByDay[11] will be the 10th day etc. If this is what you are looking for, you can use php's native range() function to make your life easier:

function display_earning() {
  $profit = $profit_roi;
  $dateBegin = now();
  $dateEnd = "date of 4 month from now";
  $totalEarning = 0; //renamed to $totalEarning
  $earningsByDay = array();
  $totalDays = 124; 

  return range(0, $profit, $profit/$totalDays)
}

A few final thoughts:

  • You say you want to display the amount earned each day. You function does not display anything it just calculates a value and returns it. That's good practise. Have your functions do one thing. Some other function can display the data from this one, or perhaps multiple functions can, each formatting it in a way that is appropriate for your current need. I can't help you with the display as I have no way of knowing what format you want. $dateEnd probably doesn't need to be in the function. On a related note, another function name might lead to less confusion about what the function does.

  • I'm not sure what $dateBegin = now(); adds, unless as Moeez Saiyam suggests, you are trying to automate this somehow.

  • You've defined $profit = $profit_roi; without declaring what $profit_roi is in the scope of your function. I know it was in your introductory notes, but the function won't know that.

  • Is the function the right place to define $profit and $totalDays? They are probably passed to the function form elsewhere.

Combining these thoughts, this gives us:

function getIncrementalEarnings($profit, $totalDays) { //function renamed to clarify its purpose
  return range(0, $profit, $profit/$totalDays)
}
Mehmet Karatay
  • 269
  • 4
  • 11
  • Thanks alot Mehmet, this is very close. my code is actually not complete. I just gave clear view of what i want. but how i can display just like you showed in the array. if you registered today. You will been shown index[0] of the array. but how do i show him assuming he logs in on the 20th day. the index[19] accumulated sum of indexes from 0 to the current day – philipsniche Jun 03 '20 at 11:01
  • I assume you know what day the customer registered their account. If so you could calculate the number of ```$days``` since registration using ideas from here: https://stackoverflow.com/questions/2040560/finding-the-number-of-days-between-two-dates. You could then access ```$index[$days]```. If the daily profit is always the same and not compounded, you could simply calculate it without having to fill in an array first: ```$profit_to_date = $profit_per_day * $days;``` In your code this is equivalent to ```$profit_to_date = $profit/124 * $days```. – Mehmet Karatay Jun 03 '20 at 15:01
  • thanks. The problem i have is to change the value display everyday, by appending the $profit_to_date toe the previous value. for this to only happen incrementally daily. like 1/6/2020 display $profit_to_date = 0.65, 2/6/2020 display $profit_to_date += 0.65 i.e. 1.3, 3/6/2020 display $profit_to_date += 0.65 this adds to the previous. the challenge is how to make this follow date increment starting from date of registration till 31/9/2020 – philipsniche Jun 03 '20 at 19:48