I have start date and end date.
I need to find out the day that is Sunday or Monday etc dependent upon user click on check box.
How can I find/calculate that in PHP?
To apply the SUMPRODUCT function for counting the weekdays we need to follow these steps: Select cell G3 and click on it. Insert the formula: =SUMPRODUCT(--(WEEKDAY(Date,2)=F3)) Press enter.
This question is just crying out for an updated answer that uses PHP's DateTime classes, so here it is:-
/**
 * @param String $dayName eg 'Mon', 'Tue' etc
 * @param DateTimeInterface $start
 * @param DateTimeInterface $end
 * @return int
 */
function countDaysByName($dayName, \DateTimeInterface $start, \DateTimeInterface $end)
{
    $count = 0;
    $interval = new \DateInterval('P1D');
    $period = new \DatePeriod($start, $interval, $end);
    foreach($period as $day){
        if($day->format('D') === ucfirst(substr($dayName, 0, 3))){
            $count ++;
        }
    }
    return $count;
}
You could create a function that uses strtotime() recursively to count the number of days. Since strtotime("next monday"); works just fine.
function daycount($day, $startdate, $counter)
{
    if($startdate >= time())
    {
        return $counter;
    }
    else
    {
        return daycount($day, strtotime("next ".$day, $startdate), ++$counter);
    }
}
echo daycount("monday", strtotime("01.01.2009"), 0);
Hopefully this is something you're looking for :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With