Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6689583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:32:11+00:00 2026-05-26T05:32:11+00:00

PHP: How to sum the result of a foreach statement? Below is my working

  • 0

PHP: How to sum the result of a foreach statement?

Below is my working code. As you will see, I’m trying to count the days in each month and then sum them.

//  A function to calculate days in a given month using just the date
function daysInMonths($date)
{
    $month = date("m", strtotime($date));
    $year =  date("Y", strtotime($date)); 
    $num = cal_days_in_month(CAL_GREGORIAN, $month, $year); 
    return $num;
}       

// A function that places the date of an unknown number of months into an array:
function getNextMonths($date, $numberOfMonths)
{
    $timestamp_now = strtotime($date);
    $months[] = date('Y-m-d', $timestamp_now);

    for($i = 1;$i <= $numberOfMonths; $i++)
        {
            $months[] = date('Y-m-d', (strtotime($months[0].' +'.$i.' month')));
        }

// counts the days in each month:
    $j=0;
    foreach ($months as $days)
    {
        echo "$j:".daysInMonths($days)."<br>";
        ++$j;            
    }
    print_r($months);
}

getNextMonths('2011-11-1', '4');

Current output:

Array ( 
    [0] => 2011-11-01 
    [1] => 2011-12-01 
    [2] => 2012-01-01 
    [3] => 2012-02-01 
    [4] => 2012-03-01 
)

After counting:
0:30
1:31
2:31
3:29
4:31

This is all correct, I’m just having trouble summing the array after I have the days of the month counted.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:32:12+00:00Added an answer on May 26, 2026 at 5:32 am
    $tot = 0;
    foreach ($months as $days) {
        $tot += daysInMonths(days);
    }
    echo $tot;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert the following python code to a PHP code. Can
In my PHP code I have a variable that corresponds to the sum and
Possible Duplicate: increment date by one month I have the following code to sum
I'm trying to get the sum of a column. My Schema is as below...
How can I get count(*) or SUM(col1) using PHP PDO I have used $STH_SELECT
Hi I have a query in PHP that calculates the sum of all the
$.getJSON(./data/revenue-item-data.php,{val:'val'},function(data){ $('#totalUnits').val(data[0].SUM(item_qty)); $('#totalAmounts').val(data[0].SUM(DISTINCT net_total)); }); Here is array I get for above code: [{SUM(item_qty):68,SUM(DISTINCT
PHP code: echo date(c); //out put like this 2012-06-19T20:37:44+05:30 echo date(d M, Y); //out
PHP experts, I've been working on this problem for about a day and a
here is problem about sum of factorial of digit http://projecteuler.net/index.php?section=problems&id=254 also here is Define

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.