Get the number of days between two dates in PHP
$startDate = new DateTime("2019-10-27");
$endDate = new DateTime("2020-04-11");
$difference = $endDate->diff($startDate);
echo $difference->format("%a");
Get the number of days between two dates in PHP
$startDate = new DateTime("2019-10-27");
$endDate = new DateTime("2020-04-11");
$difference = $endDate->diff($startDate);
echo $difference->format("%a");
how to calculate days between two dates in php
<?php
function dateDifference($start_date, $end_date)
{
// calulating the difference in timestamps
$diff = strtotime($start_date) - strtotime($end_date);
// 1 day = 24 hours
// 24 * 60 * 60 = 86400 seconds
return ceil(abs($diff / 86400));
}
// start date
$start_date = "2016-01-02";
// end date
$end_date = "2016-01-21";
// call dateDifference() function to find the number of days between two dates
$dateDiff = dateDifference($start_date, $end_date);
echo "Difference between two dates: " . $dateDiff . " Days ";
?>
php calculate weeks between two dates
function numWeeks($dateOne, $dateTwo){
//Create a DateTime object for the first date.
$firstDate = new DateTime($dateOne);
//Create a DateTime object for the second date.
$secondDate = new DateTime($dateTwo);
//Get the difference between the two dates in days.
$differenceInDays = $firstDate->diff($secondDate)->days;
//Divide the days by 7
$differenceInWeeks = $differenceInDays / 7;
//Round down with floor and return the difference in weeks.
return floor($differenceInWeeks);
}
$numOfWeek = numWeeks('2021-01-21', '2021-01-28');
Number of week days between two dates in php
You are given two string (dd-mm-yyyy) representing two date,
you have to find number of all weekdays present in between given dates.
function number_of_days($days, $start, $end) {
$start = strtotime($start); $end = strtotime($end);
$w = array(date('w', $start), date('w', $end));
$x = floor(($end-$start)/604800);
$sum = 0;
for ($day = 0;$day < 7;++$day) {
if ($days & pow(2, $day)) {
$sum += $x + ($w[0] > $w[1]?$w[0] <= $day || $day <= $w[1] : $w[0] <= $day && $day <= $w[1]);
}
}
return $sum;
}
function getWeeklyDayNumbers($startDate, $endDate) {
$weekdays = array('monday' => 0, 'tuesday' => 0, 'wednesday' => 0, 'thursday' => 0, 'friday' => 0, 'saturday' => 0, 'sunday' => 0);
$weekdays['monday'] += number_of_days(0x02, $startDate, $endDate); // MONDAY
$weekdays['tuesday'] += number_of_days(0x04, $startDate, $endDate); // TUESDAY
$weekdays['wednesday'] += number_of_days(0x08, $startDate, $endDate); // WEDNESDAY
$weekdays['thursday'] += number_of_days(0x10, $startDate, $endDate); // THURSDAY
$weekdays['friday'] += number_of_days(0x20, $startDate, $endDate); // FRIDAY
$weekdays['saturday'] += number_of_days(0x40, $startDate, $endDate); // SATURDAY
$weekdays['sunday'] += number_of_days(0x01, $startDate, $endDate); // SUNDAY
return $weekdays;
}
$weekdays = getWeeklyDayNumbers('01-01-2021', '31-01-2021');
print_r($weekdays);exit;
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us