mouna
mouna

Reputation: 71

get number of days in each month from difference of two dates PHP

I have two dates that belong to consecutive months, and I want to calculate the difference and how many days per month were taken. Example $start='26102022', $end='06112022'. what Im doing so far to get the number of days per month is :

$lastOfCurrentMonth=strtotime(date("Ymt",strtotime($val['datekey_start'])));
$iDays1 = numberOfWorkingdays($start, $lastOfCurrentMonth);
$iDays2 = numberOfWorkingdays($lastOfCurrentMonth, $end);
   
public function number_of_working_days($startDate, $endDate, $uid)
{
    $workingDays = 0;
    $startTimestamp = $startDate;
    $endTimestamp = $endDate;

//working days fct

public workingDays($start,$end) {
    $workingDays = 0;
    for ($i = $start; $i <= $end; $i = $i + (60 * 60 * 24)) {
        if (date("N", $i) <= 5) $workingDays = $workingDays + 1;
    }
    return $workingDays;
}

the count is sometimes 1 day shorter (so I just cant add 1) I was wondering is it the timestamp could different format give me different timestamp for the same date? and how can I make the $lastDayOfCurrent Month has the same format as 'Ymd' before using it to count days?

Upvotes: 0

Views: 30

Answers (0)

Related Questions