Add together integer values in array

I’ve got an array that out puts the value like 11100111, is there a way to add all these values together.

The function (i want to work out the total amount of days spent on a task) grabs the date values (day, month and year), they’re seperate, puts them together for the date i started the task and the date i ended the task. I know I’m problaby doing this the wrong way but as always, if you can help please do…

    function total_days(){

    $hours_spent = query("SELECT * FROM tasks");
    confirm($hours_spent);

    while($row = fetch_array($hours_spent)) {

    $start_year = $row['start_year'];
    $start_month = $row['start_month'];
    $start_date = $row['start_date'];

    $end_year = $row['end_year'];
    $end_month = $row['end_month'];
    $end_date = $row['end_date'];

    $started_on = $start_year."-".$start_month."-".$start_date ;
    $ended_on = $end_year."-".$end_month."-".$end_date ;

    $date_start= strtotime($started_on ); 
    $date_end = strtotime($ended_on); 
  
    // Get the difference and divide into  
    // total no. seconds 60/60/24 to get  
    // number of days 
    //echo "Difference between two dates: ". ($date_end - $date_start)/60/60/24; 

    $updated_start_end_day = ($date_end - $date_start)/60/60/24;
    $updated_no_of_days = $updated_start_end_day + 1;
    //echo "<p>The amount of days worked on this project is updated date ".$updated_no_of_days."</p>";

   
    echo $updated_no_of_days;

    }
}

Thanks
Darren

array_sum — Calculate the sum of values in an array

But it looks like you can do that already in the SQL.

There aren’t any arrays in the code you’ve given. It looks like you want to find the number of days between the start date and end date, is that right?

Hi,
That’s right, I’m seeing if I can make a ‘total of days spent all together’. When I echoed the value of the days spent individually it looked like 1110111 so I was thinking, is there a way to easily add these values together…

Thanks
Darren

You can use PHP’s DateTime classes to help you. First, you create your start and end DateTime objects, then use diff to get the difference as a DateInterval object.

Just change how you define $date_start and $date_end.

// Your code above these lines can stay as is...
$date_start = new DateTime($started_on);
$date_end = new DateTime($ended_on);

$difference = $date_start->diff($date_end);

echo $difference->days;

why are that even six columns in your database and not two with the datetime type? then you could just use DATE_DIFF function.

In hindsight, i should have done that. I just at the time couldn’t get my head around of making it easier for myself to update or edit a date or day without having a bloated calendar script.

The idea was to created the information for the date separately, and then merge them together to put the in the database as datetime…

I’ve added 2 screenshots…the front end and then the database, if it helps…

![27-11_10_44|690x366]

i would suggest to alter your table layout, splitting dates this way is very bad if you want to make calculations on them, as you can see, and it wont get easier.

darren_azzopardi

What am I missing here? Yes, of course hind sight is 20/20… and you should have/could have changed the table layout to accomodate things better…

but this is not a a hard ‘fix’ (unless I’m totally missing something?) for the current state of things:

Something like this implemented into your code should work:

$stringValue = '11100111';
$dayArray = str_split($stringValue);
echo 'Total Days: ' . array_sum($dayArray);

Hi,

Thanks for this. Using your example in my code it works perfectly, as in it counts the numbers together in the variable of $stringValue.

When I replace the numbers with my variable $updated_no_of_days the value that’s echo’d is 1.

$updated_start_end_day = ($date_end - $date_start)/60/60/24;
$updated_no_of_days = $updated_start_end_day + 1;

//replace numbers with variable
$stringValue = $updated_no_of_days;

$dayArray = str_split($stringValue);
}
echo 'Total Days: ' . array_sum($dayArray);

}

The value of $updated_no_of_days must not be treated as numbers in my php code, but when i hard code random numbers it works perfectly…

I’m going to have to go back to the drawing board on this and break it down.

Thank you for your help.

I guess I’m not following?

What is the value of: $updated_no_of_days

Is it a string? is a int? (or something else? HEX?0

this doesnt seem to make sense:

$updated_start_end_day + 1

What is the value of: $updated_no_of_days

and what is the updated value of:

$updated_no_of_days after this:

$updated_start_end_day + 1?

You can probably cast it to an string first?

Edit:

What gives you this (from one of your original posts?)

1110111

When you do this: $updated_start_end_day + 1

What are you doing? adding a ‘1’ to the end of the ‘string/value’?

What does this give you: ```
$updated_start_end_day = ($date_end - $date_start)/60/60/24


Is this 1 value?  or the whole string of values:  ie: 1111011?

Hi there,

Apologies for not getting back to you sooner.

I’ve looked at the coding and i’ve tried to make it simpler.

function total_days(){

$hp = query("SELECT * FROM tasks");
confirm($hours_spent);

while($row = fetch_array($hours_spent)) {

$start_date = $row['start_date'];
$end_date = $row['end_date'];


$datetime1 = date_create($start_date);
$datetime2 = date_create($end_date);
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%a');

}

}

So what i would like the code to do, is to look at each row, work out the amount of days between the start and end date and then give 1 combined total.

At the moment if i’ve got to count the amount of days on each row (it’s counting each row, not that values, i can do that part… I’ve attached a screenshot…

So the image I’ve marked the area of where i want the function to output something like …"…12 days"

Again, any help would be greatly appreciated…

Thanks
Darren

Use $interval->days to get the number of days as an integer rather than a string. format() will create a string. This isn’t always important with PHP, but it’s a good habit to get into.

function total_days()
{
    $hp = query("SELECT * FROM tasks");
    confirm($hours_spent);

    $output = 0;
    while($row = fetch_array($hours_spent)) {
        $start_date = $row['start_date'];
        $end_date = $row['end_date'];
        $datetime1 = date_create($start_date);
        $datetime2 = date_create($end_date);

        $interval = date_diff($datetime1, $datetime2);

        $output += $interval->days;
    }

    echo $output;    
}

why… what… why… what… select datediff(start_date, end_date) from table ???

1 Like

Scratch mine, that’s a nice one I didn’t know about. If you want to total everything in the table you could use:

select sum(datediff(start_date, end_date)) as total_days from tasks

That will get you a single row with the value total_days being your total.

Sponsor our Newsletter | Privacy Policy | Terms of Service