How to calculate Due Fees? - php

I want to calculate due fees, w.r.t current month. All session of schools starts from Apr, and fees starts from Apr.
If student not pay any fees in Apr, May, Jun, and now paying fees in Aug. Than Due fees must be Apr + May + Jun.
But I`m confused how to get due fees based on current month.
My code:
$current_month = date('M');
$final = 0;
for($i='Apr'; $i<$current_month; $i++)
{
$query_run = mysqli_query($conn,"SELECT amount FROM fees_on_class WHERE class = '$class'");
while ($num = mysqli_fetch_assoc ($query_run))
{
$temp += $num['amount'];
}
$final = $final + $temp;
}
mysqli_query($conn,"INSERT into dues(registration_number, due_fees)VALUES('$registration_number','$final')");
Code is not working and giving Zero (0).
Thanks.

Alphabetic value in for loop you are using will not work as you want.
for($i='Apr'; $i<$current_month; $i++)
{
}
Please make month loop numeric.
for($i='4'; $i<$current_month; $i++)
{
}
I have not checked the whole code, but this is the first error I can see..

Related

PHP social contribution Yearly

I'm working on a system report using PHP. I have a table with two columns (payment date and amount).
How I can use a while loop to calculate the total amount after each December through years? I tried to do it as per the screenshot attached but it displays the wrong sum after each December.
Below is how I did it:
<?php
$total=0;
$s = date('m');
$c = mysqli_query($con,"SELECT * FROM payroll_contribution_pssf WHERE employee_id='3' ORDER BY contribution_id ASC");
while($array = mysqli_fetch_array($c))
{
$m = date('m', strtotime($array['payroll_date']));
echo $array['payroll_date']." ".$array['employee_amount']."<br>";
if($m==12)
{
if($total == 0)
$total = $array['employee_amount'];
$total = $total + $array['employee_amount'];
echo number_format($total,0)."<br>";
}
}
?>
It seems you want it to calculate and display the total value of all the previous rows since the last total was calculated (or since the start of the dataset, in the case of the first iteration).
In that case the flaws in your logic are that you're
a) only adding to the total when it's the 12th month, and
b) you're not resetting it after you've reached the 12th month.
You need to have a "total" variable which is incremented each time you loop, and also you need to reset it to 0 after displaying it.
For example:
$total = 0;
$c = mysqli_query($con,"SELECT * FROM payroll_contribution_pssf WHERE employee_id='3' ORDER BY contribution_id ASC");
while ($array = mysqli_fetch_array($c))
{
$m = date('m', strtotime($array['payroll_date']));
echo $array['payroll_date']." ".$array['employee_amount']."<br>";
$total += $array['employee_amount']; //increment every time
if ($m == 12)
{
echo number_format($total, 0)."<br>";
$total = 0; //reset after displaying
}
}

php - how to increment a user's known earning every day for 4 months

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;
}
}
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.
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)
}

auto generated purchase order number base from year in laravel

I have this code which should generate purchase number automatically with year attached to the number.
$record = Ponumbers::latest()->first();
$expNum = explode('-', $record->purchase_num);
//check first day in a year
if ( date('l',strtotime(date('Y-01-01'))) ){
$nextPoNumber = 'po'.date('Y').'-000001';
} else {
//increase 1 with last invoice number
$nextPoNumber = $expNum[0].'-'. $expNum[1]+1;
}
but it doesn't seems to work because it display only the this,
I am guessing it only displayed what's in this line
$nextPoNumber = 'po'.date('Y').'-000001';
any suggestion? thanks you so much in advance!
date('l',strtotime(date('Y-01-01'))) returns first weekday name for 2019, which is Tuesday. That is truthy value so the if is always true and $nextPoNumber is always po2019-000001.
If I understand your code correctly this is what you like it to do:
$record = Ponumbers::latest()->first();
$expNum = explode('-', $record->purchase_num);
$nextPoNumber = 'po'.date('Y').'-'.sprintf("%06d",$expNum[1]+1);
Try this :
$record = Ponumbers::latest()->first();
$expNum = explode('-', $record->purchase_num);
//check first day in a year
$first_day = gmdate('j', strtotime('first day of january this year'));
if (date('j') == $first_day){
$nextPoNumber = 'po'.date('Y').'-000001';
} else {
//increase 1 with last invoice number
$nextPoNumber = $expNum[0].'-'. $expNum[1]+1;
}

Recursive infinite daily dynamic loop array php

I am trying to display a number every day in a loop. After the last element is reached it needs to get to the first element again. This needs to happen daily. I have overworked my brains out but did not managed to solve it. Function needs to return current number by day/hour/minute, like . This is what I tried till now.
<?php
function recursive_daily_deals($i = 1) {
$current_date = strtotime(date('d-m-Y h:i:s'));
$dbs_date_1 = strtotime('29-06-2017 8:20:16');
$current_hour = date('h');
var_dump($current_hour);
$products = [
451,
455,
453
];
if ($i < count($products)) {
return recursive_daily_deals($i+1);
}
}
?>
EXPECTED output
> First day - June 29 2017
> It will appear 451
> Second day - June 30 2017
> It will appear 455
> 3rd day - July 1st 2017
> It will appear 453
> 4th Day - July 2nd 2017
> It will appear 453
> And start over
First you need to know how many days have been since the starting day. To do that you just need to sub the starting timestamp from the actual timestamp :
$dbs_date_1 = strtotime('29-06-2017 8:20:16');
$actualTimestamp = time();
$elapsedSec = $dbs_date_1 - $actualTimestamp;
// we need to get days from seconds
$elapsedDays = $elapsedSec / (3600*24);
$elapsedDays = floor($elapsedDays);
So when you have how many days have been since the starting day. We use floor() instead of round() because if the script runs after the half of the day it will return the number of days +1.
With this number of days we can have the number of cycles already done by dividing the number of elapsed days by the number of items in our array :
$nbItems = count($products);
$cycleCount = $elapsedDays / $nbItems;
$finishedCycles = floor($cycleCount);
We store the number of finished cycles by flooring the number of cycles. Then we just have to sub the days it took to complete those cycles from the elapsed days and we will get the position of the index.
$completeDays = $finishedCycles * $nbItems;
$actualPosition = $elapsedDays - $completeDays;
return $products[$actualPosition];
While this is a simplified version of the code originally posted, I believe it contains the kind of logic that the OP seeks, as follows:
<?php
$products = [
451,
455,
453
];
$modfactor = count($products);
$days = null;
$weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
for ($i=0, $max = 7; $i < $max; $i++) {
$days[$i] = $i % $modfactor;
}
foreach ($weekdays as $dex => $wday) {
echo "$wday, ";
echo $products[ $days[$dex] ], "\n";
}
See demo
Update: See demo here which makes use of array_map() and gets the current product ID, too.
While the loop is ever present, it is not infinite. If the number of products changes, then the modfactor changes, too. What stays constant are the days of the week. What makes the code work is taking advantage of a modulo operation.

Adding new items into PHP array and saving previous size of the array

I have the following code so far:
$months = array();
$numJoin = date("n",strtotime($me['joinTime']));
$numLast = date('n', strtotime('Dec 31'));
$numCurrent = date("n",strtotime('2016-06-01'));
array_push($months, date("F", strtotime($me['joinTime'])));
for($i = ($numJoin + 1); $i <= $numLast; $i++) {
if($numCurrent>$numJoin) {
$dateObj = date_create_from_format('!m', $i);
array_push($months, $dateObj->format('F'));
}
$numCurrent= -1;
}
What I'm trying to do here is to add into the array current month that kicks in, and save previous months in the array like for example:
Start month is -> May
June kicks in -> I add June into the array (now I should have May and June in array).
July kicks in -> I add July into the array (now I should have May, June and July in array).
How can I do this achieve this? Current solution works only for +1 month.. I can't add more than 1 month :/
P.S. New item should only be added when the new month kicks in, and previous content of the array should be saved...
Here we go, you need to check that your month is less than the current month or not. Check Online
$months = array();
$num = date("n",strtotime($me['joinTime'])); //join month number
$now = date("n"); //Current month number
for($i = $num; $i <= $now; $i++){
$dateObj = DateTime::createFromFormat('!m', $i);
array_push($months, $dateObj->format('F'));
}
print_r($months);
I'm still a little confused, but I think this is what you are after... all month names after the join month and until current month...
$me = array('joinTime'=>'2016-03-01');
$dtCurrent = strtotime($me['joinTime']);
$arrMonths = array();
while($dtCurrent < time()) {
$dtCurrent = strtotime("+1 month",$dtCurrent);
$arrMonths[] = date('F',$dtCurrent);
}
var_dump($arrMonths);

Categories