I'm here trying to make a scenario, where I visit a juice shop to ask the juice seller to provide juice until a certain date, by providing him a certain amount he will provide me juice service until by balance will over.
$rate = 30; //rate of per juice glass
$total_amt = 200; //amt that provide to the seller at start of the service
$start_date = "19-08-2022"; //date of starting the service
$cost = $rate * 7; //rate * days
if($cost <= $total_amt){
echo "Remainder:".$total_amt - $cost; //Money that seller give back to buyer
} else {
echo "you need to pay extra:".$cost - $total_amt; //money need to pay exta for glass
}
I'm unable to figure out, how can I calculate the number of glass that comes inside $200 based upon the service start date and the next due date.
Example:
In $200 amount, the buyer will get 6 glass with returing balance $20, because If the buyer service start date is 19-08-2022 then the due date is 25-08-2022 (19 6 = 25)
So, the output needs to be like:
Number of glass in $200 is: 6
returning balance : $20
next due date: 25-08-2022
CodePudding user response:
Calculation of the due date:
- GIVEN a start date (as in your example)
- GIVEN the interval (as in your example)
You can easily calculate the according target date via:
- Initiate a DateTime object, with your given start date, according to your specified format:
DateTime::createFromFormat("d-m-Y", "19-08-2022")
- Create your DateInterval. Given an amount of days, for example two days, the according string you need to pass would need to be:
P2D
:
new DateInterval('P2D')
- Now you just need to add your
DateInterval
onto yourDateTime
via the add method, and you get your resultingDateTime
object:
var_dump( DateTime::createFromFormat("d-m-Y", "19-08-2022")->add(new DateInterval('P2D')) );
Calculation of the amount of glasses that can be afforded:
If one class costs $glass_price
, and you have a total amount of $money_available
, you can simply calculate the amount of glasses you can afford via:
floor($money_available / $glass_price)
Calculation of the remaining amount:
And for the returning balance, you would do:
$money_available % $glass_price
CodePudding user response:
Thanks, @DevelJoe for providing the way to calculate the next due date
$rate = 30;
$total_amt = 200;
$start_date = "19-08-2022";
$days = floor($total_amt / $rate); // provide number of days
$glasses = $days; //each day means 1 glass
$cost = $rate * $days; //calculating the per day cost with days
$due_date = DateTime::createFromFormat("d-m-Y", $start_date)->add(new DateInterval('P'.$days.'D'));
if($cost <= $total_amt){
$bal = $total_amt % $cost;
var_dump(array("glasses"=>$glasses, "buyer_bal"=> $bal, "due_date"=>$due_date->format('d-m-Y')));
} else {
$bal = $cost % $total_amt;
var_dump(array("glasses"=>$glasses, "buyer_bal"=> $bal, "due_date"=>$due_date->format('d-m-Y')));
}
Output
array(3) {
["glasses"]=>
float(6) //<-----------Glasses
["returing_bal"]=>
int(20) //<------------Return Balance
["due_date"]=>
object(DateTime)#1 (3) {
["date"]=>
string(26) "2022-08-25 14:09:02.000000" //<----Next due date
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
}