How to create a list of due dates which are dynamically added base from approved date in Laravel? - laravel

I am doing a credit collection app which offer loans to the borrowers. I've been stock in this for so many days now because I can't think of a better way to do it.
SO basically I want to achieve is to have a list of due dates which I will display in the borrower dashboard. for example, the borrower applied for a 2 months loan duration and bi-monthly payment scheme and let say the admin approve it today. So what I want is base from the date today, 12-10-2021 the system will generate 4 due dates(12-25-2021, 12-8-2021, 1-23-2022 and 02-7-2022) and will save them to a separate due dates table.
With regards to the payments, I can analyze the part where the system can check if the payment is done on or before the first due date but how can I tell to the system that the payment is advance payment for the next 3 due date?
what I only have right now is this
$date_approval = Carbon::createFromTimestamp(strtotime($loan_application->date_approval));
$scheme_numdays = $loan_application->scheme->num_days;
$days = (intdiv($date_approval->diff(Carbon::now())->days , $scheme_numdays) + 1) * $scheme_numdays
$due_date = $date_approval->addDays($days)->format('M d Y');
Which can identify the first due date.
Can you help to point to any resources out there than will help me achieve my objective? I am hoping I was able to describe the question and my objective clearly. Any suggestions will be very much appreciated. Thank you so much in advance!
RESPONSE FOR #GRANT
I am following your code
$date_approval = Carbon::createFromTimestamp(strtotime($loanapplication->date_approval));
$scheme_numdays = $loanapplication->scheme->num_days;
$days = (intdiv($date_approval->diff(Carbon::now())->days , $scheme_numdays) + 1) * $scheme_numdays;
$due_date = $date_approval->addDays($days)->format('Y-m-d');
$due_date_plus = $date_approval->addDays(60)->format('Y-m-d');
# Carbon Period usage:
$result = CarbonPeriod::create($due_date, $scheme_numdays.' days', $due_date_plus);
I replace the '# days' in the $result with $scheme_numdays so which contains the number of days.
Then I display it like this,
<div class="col-md-6">
{{$date_approval}}<br/>
{{$loanapplication->date_approval}}<br/>
#foreach ($result as $dt)
{{$dt->format("M d Y")}} <br/>
#endforeach
</div>
the result is
2022-02-27 00:00:00
2021-12-14
Dec 29 2021
Jan 13 2022
Jan 28 2022
Feb 12 2022
Feb 27 2022
Question:
why {{$date_approval}} (2022-02-27 00:00:00) and {{$loanapplication->date_approval}} (2021-12-14) gave different result? The date 2021-12-14 is what I have in my database.
My second question is why the result is five instead of 4 only?
The Data I'm displaying is, the loan duration is 60 days and the payment scheme is on every 15 days or fortnightly.

CarbonPeriod would be perfect for this particular scenario. Have you tried the following:
$date_approval = Carbon::createFromTimestamp(strtotime($loan_application->date_approval));
$scheme_numdays = $loan_application->scheme->num_days;
$days = (intdiv($date_approval->diff(Carbon::now())->days , $scheme_numdays) + 1) * $scheme_numdays
$due_date = $date_approval->addDays($days)->format('Y-m-d');
$due_date_plus = $date_approval->addMonths(8)->format('Y-m-d');
# Carbon Period usage:
$result = CarbonPeriod::create($due_date, '2 months', $due_date_plus);
foreach ($result as $dt) {
echo $dt->format("M d Y");
}
You may need to namespace it:
use Carbon\CarbonPeriod;
Edited answer for followup questions:
<div class="col-md-6">
{{ $date_approval->format('M d Y') }}<br/>
{{ $loanapplication->date_approval->format('M d Y') }}<br/>
#foreach ($result as $dt)
#if($loop->index > 3)
#break
#endif
{{ $dt->format('M d Y') }}<br />
#endforeach
</div>
An alternative is date casting when getting dates from models.
(in loan application model):
protected $casts = [
'date_approval' => 'datetime:M d Y',
];

Related

How to calculate experience by using date with current date in laravel?

I am using laravel framework to develop api's , i have one column inside table with timestamp,i am fetching that value and i want to show that value to the 3 Years 2 Month or if it's been in days it should show 10days, i have tried by using carbon package to convert as per my requirement but i can't able to figure out can you please help me to achieve this one.
Ex1:-
$date = 2022-09-15 00:00:00;
//my expectation is **8days**
Ex2:-
$date = 2021-08-23 00:00:00;
//my expectation is 1 year 1 month
Here is the example you can do like this
$Born = Carbon\Carbon::create(1986, 1,3);
$Age = $Born->diff(Carbon\Carbon::now())->format('%y Year, %m Months and
%d Days');
echo $Age;
Here is your date example is working and it's result
$date1 = \Carbon\Carbon::create("2022-09-15 00:00:00");
$date2 = \Carbon\Carbon::create("2021-08-23 00:00:00");
$totalYearMonthDate = $date1->diff($date2)->format('%y Year,
%m Months and %d Days');
Result:-
1 Year, 0 Months and 23 Days
diffForHumans has parts and minimumUnit options that do what you want:
$options = [
'parts' => 2,
'minimumUnit' => 'day',
'skip' => ['week'],
];
echo Carbon::parse('2022-09-15 00:00:00')->diffForHumans($options) . "\n";
echo Carbon::parse('2021-08-23 00:00:00')->diffForHumans($options) . "\n";
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference ". $interval->y . " years, " .
$interval->m." months, ".$interval->d." days ";
Try doing like this

getting hours and day difference between two dates in codeigniter

i am getting two dates as
$date1 = 2020-07-16 03:50:32
$date2 = 2017-01-25 09:43:53
i want to get the difference between thes two dates.
THe difference count hours until 24 hours and then days plus hours.
eg. 2 days and 5 hours.
THe code i tried is this
$createddate = date("d-m-Y H:i:s", strtotime($application['created_at']));
$approvedisapprovedate = date("d-m-Y H:i:s", strtotime($application['approved_at']));
$created = strtotime($createddate);
$approvedisapprove = strtotime($approvedisapprovedate);
$diff = $approvedisapprove - $created;
$days = floor($diff / (60 * 60 * 24));
$hours = round(($diff - $days * 60 * 60 * 24) / (60 * 60));
but it won't work.anybody suggest a solution.
You can use carbon for this. Carbon can make it simple to manipulate dates very efficiently not for only this task but many other related date.
Install carbon
{
"require": {
"nesbot/carbon": "^1.22"
}
}
Usage
$date->diffForHumans();
OR
$date->diffInDays();
In your case, you can try below code -
$createddate = Carbon::parse($application['created_at']);
$approvedisapprovedate = Carbon::parse($application['approved_at']);
//here is carbon magic
$createddate->diffInDays($approvedisapprovedate); //This will give you diff in number of days.
$createddate->diffForHumans($approvedisapprovedate); //This will give you diff which is readable by human. ex- 1 day 2 hours
You can refer Carbon.
Hope this will help for you.
I propose you the DateInterval::format function of php
like this:
$createddate = new DateTime('12-07-2020 12:10:01');
$approvedisapprovedate = new DateTime('16-07-2020 13:15:40');
$interval = $approvedisapprovedate->diff($createddate);
echo $interval->format('%a days %h hours %i minutes')."\n";
It goes back to this:
4 days 1 hours 5 minutes
I don't know the format of the date $application['created_at'] and $application['approved_at'] but I just put it there:
$createddate = new DateTime($application['created_at']);
$approvedisapprovedate = new DateTime($application['approved_at']);
$interval = $approvedisapprovedate->diff($createddate);
echo $interval->format('%a days %h hours %i minutes')."\n";
voici le lien de DateInterval::format :
https://www.php.net/manual/fr/dateinterval.format.php

Cypress: Extract text and number from variable

we have an element on our booking page ".ItineraryDate [datetime]:last" which displays the date of the last flight in the itinerary (e.g. Tue 7 Jul). For the purpose of a Cypress test, I need to extract the day (e.g. 7) and the month (e.g. Jul).
cy.get(".ItineraryDate [datetime]:last").then($span => {
const lastLegDate = $span.text()
})
This code creates const Tue 7 Jul, but I need to use only the day and the month. Do you know how could I extract it?
Cypress automatically includes moment.js and exposes it as Cypress.moment.
Refer moment formats
You can extract the month and day in anyway you want using Cypress.moment.
Markup:
<div class="date">Tue 7 Jul</div>
Test:
cy.get('.date').then(($el) => {
cy.log('original: ' + $el.text());
const dateString = Cypress.moment($el.text(), 'ddd D MMM')
const month = Cypress.moment(dateString, 'ddd D MMM').format('MMMM');
const day = Cypress.moment(dateString, 'ddd D MMM').format('D');
cy.log(`After formatting month = ${month}`);
cy.log(`After formatting day = ${day}`);
expect(month).to.equal('July');
expect(day).to.equal('7');
});
Screenshot:

Magento : Change date format for an attribute

The date format for my begin_date attribute is displaying like : 2015-04-25 00:00:00
and i want it like : 25 04 2015
I tried :
<?php
echo $this->helper('core')->formatDate(getbegin_date(), Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
?>
But it don't work.
Here is how you can format the time (in PHP). If you need more reference then check this page: Playing with Dates in Magento
$current_time = Mage::getModel('core/date')->timestamp(time());
echo $date = date('d m Y', $current_time);

Smarty if statement with date?

If I have a smarty variable
{$test.date_created}
that displays a date in a smarty template, for example:
02/2/2012
On the smarty website, it says that you can display today's date with the following.
{$smarty.now|date_format:'%m/%d/%Y'}
How can I use it in an if statement to display a message when its 3 days old or more from today's date?
If its 3 days old or more, display "Old". If not, Display "New".
You can use strtotime() to get the timestamp corresponding to three days ago, and then compare this to the date of your message. For example, assuming $message is your message record and $message['date'] is the timestamp you have to check:
$isMessageOld = ($message['date'] <= strtotime('-3 days'));
$smarty->assign('isMessageOld', $isMessageOld);
And then, in your template:
{if $isMessageOld} ... {/if}
I'm not 100% sure, but you can also test it directly in Smarty. Assuming you have $message passed to Smarty:
{if $message.date <= strtotime('-3 days')} ... {/if}
You can easily check already passed date in smarty by using this $smarty.now
$smarty.now|date_format:"%Y%m%d"
Here is an example of cross the old(passed) date
<{foreach from=$meetings item=m}>
<{if $smarty.now|date_format:"%Y%m%d" <= $m.date|date_format:"%Y%m%d"}>
<tr>
<td><{counter}></td>
<td><{$m.title}> on </td>
<td><{$m.date}></td>
</tr>
<{else}>
<tr>
<td><strike><{counter}></strike></td>
<td><strike><{$m.title}> on </strike></td>
<td><strike><{$m.date}></strike></td>
</tr>
<{/if}>
<{/foreach}>
We need to create meeting list array in php and assign it in smarty
$meetings[0]['title'] = "Speech on Gandhi Janyanti";
$meetings[0]['date'] = "2-Oct-2011";
$meetings[1]['title'] = "Meet friend";
$meetings[1]['date'] = "10-Oct-2013";
$meetings[2]['title'] = "Goto USA";
$meetings[2]['date'] = "22-Oct-2013";
$meetings[3]['title'] = "Speech on Gandhi Janyanti";
$meetings[3]['date'] = "2-Oct-2014";
$meetings[4]['title'] = "Meeting with John";
$meetings[4]['date'] = "22-Oct-2014";
$meetings[5]['title'] = "Speech on Gandhi Janyanti";
$meetings[5]['date'] = "2-Oct-2015";
$meetings[6]['title'] = "Meeting with Uncle";
$meetings[6]['date'] = "22-Oct-2015";
$theme->assign("meetings",$meetings);
echo $theme->fetch("index.tpl");

Resources