xxxxxxxxxx
Current date is between two dates
To calculate the difference between two dates in PHP, you can use the date_diff() function, which returns a DateInterval object that represents the difference between two DateTime objectshttps://www.w3schools.com/PHP/func_date_date_diff.asp
xxxxxxxxxx
$date1 = new DateTime(date('Y-m-d', strtotime($loan['LoanRepayment'][0]['od_date'])));
$date2 = new DateTime(date('Y-m-d'));
$diff = date_diff($date1, $date2);
$overdue = $diff->days;
xxxxxxxxxx
// how to calculate days difference between two dates in laravel
use DateTime; // inside Controller Class
$startDate = new DateTime($request->start_date);
$endDate = new DateTime($request->end_date);
$daysDifference = ($startDate->diff($endDate)->days);
xxxxxxxxxx
$firstDate = "2019-01-01";
$secondDate = "2020-03-04";
$dateDifference = abs(strtotime($secondDate) - strtotime($firstDate));
$years = floor($dateDifference / (365 * 60 * 60 * 24));
$months = floor(($dateDifference - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($dateDifference - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 *24) / (60 * 60 * 24));
echo $years." year, ".$months." months and ".$days." days";
//output: 1 year, 2 months and 3 days
xxxxxxxxxx
$date1 = "2007-03-24";
$date2 = "2009-06-26";
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
printf("%d years, %d months, %d days\n", $years, $months, $days);
xxxxxxxxxx
phpCopy$firstDate = "2019-01-01";
$secondDate = "2020-03-04";
$dateDifference = abs(strtotime($secondDate) - strtotime($firstDate));
$years = floor($dateDifference / (365 * 60 * 60 * 24));
$months = floor(($dateDifference - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($dateDifference - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 *24) / (60 * 60 * 24));
echo $years." year, ".$months." months and ".$days." days";
//output: 1 year, 2 months and 3 days
xxxxxxxxxx
/**
* takes two dates and returns an array of dates between them
*
* @param date1 The start date of the period.
* @param date2 The end date of the period.
* @param format The format of the date you want to display.
*
* @return array array of dates between the two dates.
*/
function displayDatePeriod( $date1, $date2 , $format = "Y-m-d"){
$periodArray = [];
$period =
new \DatePeriod(
new \DateTime($date1),
new \DateInterval('P1D'),
new \DateTime($date2)
);
foreach ($period as $key => $value) {
$periodArray[] = $value->format($format) ;
}
$periodArray[] = $date2 ;
return $periodArray;
}
1
xxxxxxxxxx
$period = new DatePeriod(
new DateTime('2010-10-01'),
new DateInterval('P1D'),
new DateTime('2010-10-05')
);
//Which should get you an array with DateTime objects.
//To iterate
foreach ($period as $key => $value) {
//$value->format('Y-m-d')
}