charlie
charlie

Reputation: 481

While looping a range of integers, convert integer representing number of months to the number of years and months

I am using a for loop to display numbers 1-60:

for ($i = 0; $i <= 60; $i++)

within the loop, i want to be able to show the number of years and months. for example:

1 month
2 months
3 month
...
1 year
1 year 1 month
1 year 2 month

and so on...

I tried this:

if (!is_float($i / 12)) {
    $years = $i/12;
} else {
    $years = 'no';
}

This shows 1 on the 12 month, 2 on 24 months but not the in between.

Upvotes: 0

Views: 4204

Answers (5)

mickmackusa
mickmackusa

Reputation: 48091

For cleanly written printing script, pass the integer value of the division of the counter by 12 and the modulus/remainder of the same division to printf().

Code: (Demo)

for ($i = 1; $i <= 60; ++$i) {
    printf("Year: %d, Month: %d\n", intdiv($i, 12), $i % 12);
}

Output:

Year: 0, Month: 1
Year: 0, Month: 2
...
Year: 0, Month: 11
Year: 1, Month: 0
Year: 1, Month: 1
Year: 1, Month: 2
...

If you want to handle plurality in your output, then cache the calculated values and check if they equal 1 to determine the inclusion of s.

Code: (Demo)

for ($i = 1; $i <= 60; ++$i) {
    $years = intdiv($i, 12);
    $months = $i % 12;
    printf(
        "%d year%s, %d month%s\n",
        $years,
        $years !== 1 ? 's' : '',
        $months,
        $months !== 1 ? 's' : ''
    );
}

Output:

0 years, 1 month
0 years, 2 months
...
0 years, 11 months
1 year, 0 months
1 year, 1 month
1 year, 2 months
...

Finally, if you want to omit 0 value units, conditionally accumulate non-zero results into a temporary array and present by imploding with a delimiter.

Code: (Demo)

for ($i = 1; $i <= 60; ++$i) {
    $result = [];
    $years = intdiv($i, 12);
    if ($years) {
        $result[] = $years . ' year' . ($years !== 1 ? 's' : '');
    }
    $months = $i % 12;
    if ($months) {
        $result[] = $months . ' month' . ($months !== 1 ? 's' : '');
    }
    echo implode(', ', $result) . "\n";
}

Output:

1 month
2 months
...
11 months
1 year
1 year, 1 month
1 year, 2 months
...

Upvotes: 0

Zolt&#225;n S&#252;le
Zolt&#225;n S&#252;le

Reputation: 1702

another solution 1:

for ($i=0; $i<=60; $i++) {
    $output = [];
    if ($i >= 12) {
        $years    = ($i - $i % 12) / 12;
        $output[] = $years > 1 ? $years . ' years' : $years . ' year';
    }

    if ($i % 12 > 0) {
        $monthsRest = $i % 12;
        $output[]   = $monthsRest > 1 ? $monthsRest . ' months' : $monthsRest . ' month';
    }
    echo implode(' ', $output);
}

another solution 2:

for ($i=0; $i<=60; $i++) {
    $output    = [];
    $startDate = new DateTime();
    $endDate   = new DateTime('+' . $i . ' months');

    $interval  = $startDate->diff($endDate);
    $years     = $interval->format('%y');
    $months    = $interval->format('%m');

    if ($years > 0) {
        $output[] = $years > 1 ? $years . ' years' : $years . ' year';
    }

    if ($months > 0) {
        $output[] = $months > 1 ? $months . ' months' : $months . ' month';
    }

    echo implode(' ', $output);
}

Upvotes: 0

Lord Stiltskin
Lord Stiltskin

Reputation: 133

function yearfm($months)
{
  $str = '';

  if(($y = round(bcdiv($months, 12))))
  {
    $str .= "$y Year".($y-1 ? 's' : null);
  }
  if(($m = round($months % 12)))
  {
    $str .= ($y ? ' ' : null)."$m Month".($m-1 ? 's' : null);
  }
  return empty($str) ? false : $str;
}
for($x = 0; $x < 100; $x++)
{
  var_dump(yearfm($x));
}

Upvotes: 0

charlie
charlie

Reputation: 481

I have used the following solution to complete my code with help from @scaisEdge

for($i=0; $i<=60; $i++) {
    if(!is_float($i/12)) {
        $years = floor($i / 12).' Year';
        $years = $years.($years > 1 ? 's' : '');
        if($years == 0) {
            $years = '';
        }
    }
    $months = ' '.($i % 12).' Month';
    if($months == 0 or $months > 1) {
        $months = $months.'s';
    }

    $display = $years.''.$months;
    echo '<option value="'.$i.'"';
    if($result["warrenty"] == $i) {
        echo 'selected="selected"';
    }
    echo '>'.$display.'</option>';
}

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133410

You can use % and / for the integer part and the rest of division try this loop for show the result

 for($i=1; $i<=60; $i++){

        echo 'year = ' . floor($i/12) . ' month = ' .$i%12 . '<br />';

 }

Upvotes: 5

Related Questions