Codeigniter : Adding 2 variables to array in a for loop - php

I have:
for ($i=0; $i < 10; $i++) {
$date = Date('Y-m-d', strtotime("-$i days"));
$numar = $this->Misc->get($date);
// here a want to add to a array $date and $number
}
When using a foreach loop on the array i want to get:
Date : X Number : Y
Date : X1 Number : Y1
Date : X2 Number : Y2

$arr_outupt = array();
for ($i=0; $i < 10; $i++) {
$date = Date('Y-m-d', strtotime("-$i days"));
$numar = $this->Misc->get($date);
$arr_output[] = array("date"=>$date, "number"=>$numar);
}
echo "<table>";
foreach($arr_output as $arr_temp)
{
echo "<tr>";
echo "<td>Date : </td>";
echo "<td>".$arr_temp['date']."</td>";
echo "<td>Number : </td>";
echo "<td>".$arr_temp['number']."</td>";
echo "</tr>";
}
echo "</table>";

Try this its quite simple
$new_array = array();
for ($i=0; $i < 10; $i++) {
$date = Date('Y-m-d', strtotime("-$i days"));
$numar = $this->Misc->get($date);
// here a want to add to a array $date and $number
$new_array[] = array('Date' => $date, 'Number' => $numar );
}

Related

Custom Month and Year inside php for-loop

I saw this question:
how to get the previous 3 months in php
My question is.. How do i output from a custom month.
I want to start from Mar 2018 (or any M Y user inputs) and it should output the next 3 (or any number user inputs) months.
Ex: Mar, Apr, May
The code below is from current month and year.
// current month: Aug 2018
for ($i = 0; $i <= 2; $i++){
$x = strtotime("$i month");
echo $dte = date('M Y', $x);
echo '<br>';
}
And the output is
Aug 2018
Sep 2018
Oct 2018
You could use the DateTime class and increment using a DateInterval object:
// Assuming these are the user inputs
$month = 11;
$year = 2015;
// We create a new object with year and month format
$date = DateTime::createFromFormat('Y m', $year . ' ' . $month);
for ($i = 0; $i <= 2; $i++){
// Output the month and year
echo $date->format('m Y') . '<br>';
// Add 1 month to the date
$date->add(new DateInterval('P1M'));
}
Output:
11 2015
12 2015
01 2016
Documentation:
DateTime::createFromFormat
DateTime::format
DateTime::add
Change it to this as below it will give you as expected see the below code
// current month: Aug 2018
$effective_date = "MAR 2018";
for ($i = 0; $i <= 2; $i++){
$x = strtotime("$i month",strtotime($effective_date));
echo $dte = date('M Y', $x);
echo '<br>';
}
This may be also helpful:
<?php
$month = 11;
$year = 2017;
$count = 15;
for ($i = 1; $i <= $count; $i++) {
$month++;
if ($month > 12) {
$month = 1;
$year++;
}
$x = DateTime::createFromFormat('Y-m', $year.'-'.$month);
echo $x->format('m-Y');
echo '<br>';
}
?>
Output:
12-2017
01-2018
02-2018
03-2018
04-2018
05-2018
06-2018
07-2018
08-2018
09-2018
10-2018
11-2018
12-2018
01-2019
02-2019
Try this code for add nth Days, Months and Years
$n = 2;
for ($i = 0; $i <= $n; $i++){
$d = strtotime("$i days");
$x = strtotime("$i month");
$y = strtotime("$i year");
echo "Dates : ".$dates = date('d M Y', "+$d days");
echo "<br>";
echo "Months : ".$months = date('M Y', "+$x months");
echo '<br>';
echo "Years : ".$years = date('Y', "+$y years");
echo '<br>';
}
You can generate the date with strtotime() function
Check this phpfiddle
<?php
$currMonth = 11;
$currYear = 2017;
$count = 15;
$currentYear = date('M Y',strtotime($currYear.'-'.$currMonth));
echo $currentYear.'<br>';
for ($i = 1; $i < $count; $i++) {
$currMonth++;
if ($currMonth > 12) {
$currMonth = 1;
$currYear++;
}
$newMonthandYear = date('M Y',strtotime($currYear.'-'.$currMonth));
echo $newMonthandYear.'<br>';
}
?>
<?php
$year = date('Y');
//start at march
$startMonth = 7;
//go forwards 2 months
$stepForwards = 22;
for ($i = $startMonth; $i <= $stepForwards; $i++){
if($i > 12 ) {
$month = $i % 12 == 0 ? 12 : $i % 12;
}
else {
$month = $i;
}
echo date("M Y", strtotime(date($year.'-'.$month.'-1'))) . "<br/>";
if($month == 12) {
$year++;
echo "<br/>";
}
}

Amortization Script

I am trying to create an amortization calculator that calculates the declining principal
$x = 1;
$starting_pmt = 26;
$ending_pmt = 36;
$i = 0.0010316264327892;
$p = 410000;
$pmt = 916.84;
$num_pmts = $ending_pmt - $starting_pmt;
echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>PMT Num</th>";
echo "<th>Balance</th>";
echo "<th>Principle</th>";
echo "<th>TTL Principle</th>";
echo "<th>Interest</th>";
echo "<th>Payment</th>";
echo "</tr>";
while ( $starting_pmt <= $ending_pmt ) {
echo "<tr><td>";
echo $starting_pmt;
echo "</td><td>";
echo $p;
echo "</td>";
echo "<td>$prin</td>";
echo "<td>$TTLprin</td>";
echo "<td>$interest</td>";
echo "<td>$TTLPmt</td> </tr>";
$starting_pmt = $starting_pmt + 1;
$p = $p -($p*$i);
$prin = $pmt - ($p * $i);
$interest = $pmt - $prin;
$TTLPmt = $prin + $interest;
//$cumTTL = $$pmt - ($p * $i);
$TTLprin = $prin + $prin;
}
echo "</table>";
?>
balance for each payment and the total principle paid between two values. I also want to calculate the interested paid for each payment and the accumulative interest paid between the two values.
This is what I am starting with.
I cannot figure out how to get the loop to do the math during each iteration............... I have been working my original code and I am getting closer. I cannot get the loop to calculate the cumulative total for principal and interest paid.
<?php
$starting_pmt = 1;
$ending_pmt = 10;
$i = 0.0010316264327892;
$p = 410000;
$pmt = 916.84;
$num_pmts = $ending_pmt - $starting_pmt;
while($x < $num_pmts) {
echo "<tr>";
echo "<td>$x</td>";
echo "<td>".$p - ($p*$i)."</td>";
echo "<td></td>";
echo "<td></td>";
echo "<tr>";
$x++;
I get the math. It's getting the while loop to do what I need. It's not calculating the cumulative total for the principle paid.
Here is a way to make an amortization script, you need to pass back what you want your rate, payment date, and all that good stuff. then I am returning an associative array with all the values for each month.
public static function amortizeLoan($principal, $rate, $term, $extra_days, $payment_date){
$payment = self::getPayment($principal,($rate*100),$term,$extra_days);
$MonthlyInterestRate = $rate / 12;
$Multiplier = 1 + $MonthlyInterestRate;
$TermRate = 1;
for($x=0;$x<$term;$x++){
$TermRate = $TermRate * $Multiplier;
}
$Days = $extra_days - 30;
$oddDayInt = $Days > 0 ? round($principal * ($rate / 365.25) * $Days,2) : 0;
$amortization = [];
$total_interest = 0;
$total_principal = 0;
$remaining_principal = $principal;
$compareDay = date('d', strtotime($payment_date));
for($x=0;$x<$term;$x++){
$this_int_paid = round((($rate)/12) * $remaining_principal,2);
if($x==0){$this_int_paid += $oddDayInt;}
$this_princ_paid = round($payment - $this_int_paid,2);
$this_rem_princ = round($remaining_principal - $this_princ_paid,2);
if($x == ($term-1)){
if($this_rem_princ < 0){
$this_princ_paid-=abs($this_rem_princ);
$payment=round($this_int_paid+$this_princ_paid,2);
}else{
$this_princ_paid+=$this_rem_princ;
$payment=round($this_int_paid+$this_princ_paid,2);
}
}
$remaining_principal -= $this_princ_paid;
$total_interest += $this_int_paid;
$total_principal += $this_princ_paid;
// *** If month days < payment date, payment date falls on the last day of that month.***
$nextMonth = strtotime("last day of next month", strtotime($payment_date));
$lastDayNextMonth = date('d', $nextMonth);
if ($compareDay >= $lastDayNextMonth) {
$payment_date_time = $x ? $nextMonth : strtotime($payment_date);
} else {
$payment_date_time = $x ? strtotime("+1 month", strtotime($payment_date)) : strtotime($payment_date);
}
if ($lastDayNextMonth > date('d', strtotime($payment_date)) && date('t', strtotime($payment_date)) < $compareDay) {
$date = date('Y-m-d', $nextMonth);
$newDate = date_create($date);
$newDate->format('Y-m-d');
$finalDate = date_date_set(
$newDate,
date_format($newDate, 'Y'),
date_format($newDate, 'm'),
$compareDay
);
$formatedDate = $finalDate->format('m/d/y');
$payment_date_time = strtotime($formatedDate);
}
$payment_date = gmdate("Y-m-d", $payment_date_time);
$amortization[] = [
"payment"=>($x+1),
"amount"=>$payment,
"principal"=>$this_princ_paid,
"interest"=>$this_int_paid,
"total_principal"=>$total_principal,
"total_interest"=>$total_interest,
"remaining_balance"=>round($remaining_principal,2),
"payment_date"=>$payment_date
];
}
return $amortization;
}

Adding to months in array

Hi I've created an array where I increment "25/11/2015" by 1 month. I have a question about how it is working. I'm currently using "strtotime('+1 month', $currentdate)". I was wondering why in my first row of the array +1month isn't added straight away. Instead I am left with NOV-15 which is what I want but I was just wondering why this happens.
currently I'm getting:
Nov-15
Dec-15
Jan-16
why am i not outputting this result:
Dec-15
Jan-16
Feb-16
Heres my code:
$date = "2015-11-25";
$start = strtotime($date);
$currentdate = $start;
$times_table = array();
for($i = 0; $i <= 3; $i++){
$times_table[$i] = array();
}
echo "<pre>";
for($i = 0; $i <= 3; $i++){
for($j = 0; $j <= 4; $j++){
if ($j == 0){
$times_table[$i][$j]= "Version 5" ;
}
else if ($j == 1){
$cur_date = date("M-y", $currentdate);
$currentdate = strtotime('+1 month', $currentdate);
$times_table[$i][$j]= $cur_date ;
echo $cur_date . ">". "<br />";
}
else{
$times_table[$i][$j]= "gary" ;
}
if ($j == 3) {
$numbers = mt_rand(1, 100);
$times_table[$i][$j]= $numbers ;
}
if ($j == 4){
if($i == 0 || $i == 3)
{
$pay = "P";
$times_table[$i][$j]= $pay ;
}
else{
$int = "I";
$times_table[$i][$j]= $int ;
}
}
}
}
What you write is what you get:
$cur_date = date("M-y", $currentdate);
$currentdate = strtotime('+1 month', $currentdate);
$times_table[$i][$j]= $cur_date ;
You set the $currentdate value to $cur_date before one month was added, and then store the $cur_date value. This is the reason why it doesnt add 1 month straight away (it is added straight away to $currentdate but not to $cur_date)

Display data in Arrray Horizontally

I've created and array and it works just how I wanted it to. The final part I wanted to attempt is to output the data horizontally but not sure how to.
<?php
$date = "2015-11-25";
$t = 0;
$startdate = "2009/06/01";
$start = strtotime($date);
$currentdate = $start;
$times_table = array();
for($i = 0; $i <= 3; $i++){
$times_table[$i] = array();
}
echo "<pre>";
for($i = 0; $i <= 3; $i++){
for($j = 0; $j <= 2; $j++){
if ($j == 0){
$times_table[$i][$j]= "Version 4" ;
}
else if ($j == 1){
$cur_date = date('Y/m/d', $currentdate);
$currentdate = strtotime('+1 month', $currentdate);
$times_table[$i][$j]= $cur_date ;
}
else{
$times_table[$i][$j]= "good" ;
}
}
}
print_r($times_table);
echo "</pre>";
?>
I'm not sure what you are trying to accomplish, but this looks like it should be in a table. For example:
<?php
$date = "2015-11-25";
$t = 0;
$startdate = "2009/06/01";
$start = strtotime($date);
$currentdate = $start;
$times_table = array();
for($i = 0; $i <= 3; $i++){
$times_table[$i] = array();
}
for($i = 0; $i <= 3; $i++){
for($j = 0; $j <= 2; $j++){
if ($j == 0){
$times_table[$i][$j]= "Version 4" ;
}
else if ($j == 1){
$cur_date = date('Y/m/d', $currentdate);
$currentdate = strtotime('+1 month', $currentdate);
$times_table[$i][$j]= $cur_date ;
}
else{
$times_table[$i][$j]= "good" ;
}
}
}
echo '<table>';
echo '<tr><th>Version #</th><th>Date</th><th>Status</th></tr>';
foreach($times_table as $times){
echo '<tr>';
foreach($times as $t){
echo '<td>',$t,'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
I should note that I would strongly discourage outputting raw data such as with print_r or var_dump for anything but debugging purposes that cannot find their way into the real world. It can be very bad for security.

Looping through an array with PHP

I want to use PHP to populate an array starting with today's date and going several days into the future. When I tried the following below all of the columns contain "2013-11-18." I have been toying with it for 2 hours, but to no avail. What am I missing?
//Get "Day 0", today if undefined
if(isset($_GET['DAY0']) == TRUE){
$day0 = new DateTime($_GET['DAY0']);
} else {
$day0 = new DateTime('today');
}
// save day0 + 7 days into into dayArray
$dayArray[0] = $day0;
for($i=1; $i<8; $i++){
$day0->modify('+1 day');
$dayArray[i]= $day0;
}
echo "<tr>";
for ($i = 0; $i < 7; $i++) {
echo "<th>".$dayArray[i]->format('Y-m-d')."</th>";
}
echo "</tr>";
Objects are passed by reference. You are assigning multiple references to the same object in your array.
If you really need all the datetime objects in the array, you could do something like this
$interval = new DateInterval('P1D');
$start = new DateTime('today');
$dayArray = [clone $start];
for ($i = 1; $i < 8; $i++) {
$dayArray[] = clone $start->add($interval);
}
Or you could just store the formatted dates as already suggested.
$interval = new DateInterval('P1D');
$start = new DateTime('today');
$dayArray = [$start->format('Y-m-d')];
for ($i = 1; $i < 8; $i++) {
$dayArray[] = $start->add($interval)->format('Y-m-d');
}
Replace two of your $dayArray[i] with $dayArray[$i]
You could save timestamps:
// save day0 + 7 days into into dayArray
$dayArray[0] = $day0->format('U');
for($i=1; $i<8; $i++){
$day0->modify('+1 day');
$dayArray[$i] = $day0->format('U');
}
echo "<tr>";
for ($i = 0; $i < 7; $i++) {
echo "<th>".date('Y-m-d', $dayArray[$i])."</th>";
}
You can create a DatePeriod like so:
if(isset($_GET['DAY0']) == TRUE){
$day0 = new DateTime($_GET['DAY0']);
} else {
$day0 = new DateTime('today');
}
$enddate = new DateTime();
$period = new DatePeriod(
$day0,
new DateInterval('P1D'),
$enddate->add(new DateInterval('P7D'))
);
echo "<tr>";
foreach ($period as $datetime) {
echo "<th>".datetime->format('Y-m-d')."</th>";
}
echo "</tr>";

Categories