简体   繁体   中英

PHP: Counting based on a date day range

Okay, how do I explain this. Let say I have an array of numbers/days.

$days = array(31,24,08,14,17);

Now I have 4 cycles with particular day range.

  • Cycle 1: 23-01
  • Cycle 2: 02-08
  • Cycle 3: 09-15
  • Cycle 4: 16-22

So now I want to count how many of the value from the days array fall on each cycle.

Cycle 1 will count as 2 since 31 and 24 falls within 23-01 Cycle 2 will count 1 (08) Cycle 3 will count 1 (14) Cycle 4 will count 1 (17)

The numbers are from days of a given date and I just have to count the days that fall in. I already can count from cycle 2-4 but having problem with cycle 1.

$cycle1 = 0;
$cycle2 = 0;
$cycle3 = 0;
$cycle4 = 0;

$days = array(31,24,08,14,17);

foreach ($days as $day)
{
    if ($day >= 23 && $day <= 01)
    {
        $cycle1++;
    }

    if ($day >= 02 && $day <= 08)
    {
        $cycle2++;
    }

    if ($day >= 09 && $day <= 15)
    {
        $cycle3++;
    }

    if ($day >= 16 && $day <= 22)
    {
        $cycle4++;
    }

}

Thanks, hope someone can shed some light on how to do it via only date('d',strtotime(datestring));range.

<?php
$days = array(31,24,14,17,1,16);
/*
Cycle 2: 02-08 =>() = 0
Cycle 3: 09-15 =>(14) = 1 
Cycle 4: 16-22 =>(17,16) = 2
Cycle 1: 23-01 =>(31,24,1) = 3
*/
$cycle1 = $cycle2 = $cycle3 = $cycle4 = 0;

foreach ($days as $day)
{
    if($day >= 23)
        $cycle1++;

    elseif($day >= 16)
        $cycle4++;

    elseif($day >= 9)
        $cycle3++;

    elseif($day >= 2)
        $cycle2++;

    else
        $cycle1++;
}
echo "cycle1=$cycle1<br>cycle2=$cycle2<br>cycle3=$cycle3<br>cycle4=$cycle4<br>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM