0

I need help, i have a while loop with dates for one month from mysql. i have calculated the difference between times. now i must sum that all differences at the end. i have tried a lot. is there any option to add all that differences without writing each day?

while($row = mysqli_fetch_assoc($sql)){  // <---- while loop makes round 30 times... 1 month long
    $usern = $row['title'];
    $arbeit = $row['description'];
    $start_com = $row['start']; // <--- DATETIME
    $end_com = $row['end']; // <----- DATETIME
    $shop = $row['category'];

            $time_from = strtotime( $start_com );
            $time_from = date("H:i", $time_from);
            $time_to = strtotime( $end_com );
            $time_to = date("H:i", $time_to);

    $dteStart = new DateTime($start_com); 
    $dteEnd   = new DateTime($end_com); 
    $dteDiff  = $dteStart->diff($dteEnd); 

    $e = MyDateInterval::fromDateInterval($dteDiff);
    $e->add($dteDiff);      // ----- this not works


    echo '.$dteDiff->format("%H:%I").';

}
// echo $e->format("%H:%I:%S"); // ------- not working...
// <---- here i need sum of the datetimes diff ---->

$dteDiff must at the end the complete diff all times. Can anyone help me?

joergi1988
  • 73
  • 1
  • 8
  • 3
    Possible duplicate of [How we can add two date intervals in PHP](http://stackoverflow.com/questions/11556731/how-we-can-add-two-date-intervals-in-php) – splash58 May 30 '16 at 11:49
  • can you provide some data example from what you have and what u need to get – Amani Ben Azzouz May 30 '16 at 12:04
  • i calculate the differences in the while of row start and row end for a month (round 30 times or 31 times). after the while i need the sum of all the differences but i have no idea how i can do that. – joergi1988 May 30 '16 at 12:11
  • i ask there is a method without write these 31 times??? when i look here : http://stackoverflow.com/questions/11556731/how-we-can-add-two-date-intervals-in-php i must write that 31 times?? – joergi1988 May 30 '16 at 12:17
  • @joergi1988 please show the declaration of `$e =` – Will B. May 30 '16 at 14:25
  • i have make this $e = MyDateInterval::fromDateInterval($dteDiff); – joergi1988 May 30 '16 at 14:30

1 Answers1

1

Assuming $e is a DateTime object your formatting is not correct for your dates. Please see http://php.net/manual/en/function.date.php

I (capital i) is daylight saving time you should use i for minutes.

S (capital s) is English ordinal suffix for the day of the month, 2 characters such as 2nd, 1st, 5th you should use s for seconds.

So you would need to use $e->format('H:i:s') to display the formatted hours, minutes, and seconds. to output 14:30:54.


Simplified

Instead of attempting to add DateIntervals, it can be simplified by setting an initial date and a compare date to get the resulting difference from.

$initialDate = new DateTime;
$compareDate = clone $initialDate;
while($row = mysqli_fetch_assoc($sql)) {
    $start_com = $row['start']; // <--- DATETIME - PAST
    $end_com = $row['end']; // <----- DATETIME - FUTURE
    $startDate = DateTime::createFromFormat('Y-m-d H:i:s', $start_com);
    $endDate = DateTime::createFromFormat('Y-m-d H:i:s', $end_com);
    if ($startDate && $endDate) {
        $initialDate->add($startDate->diff($endDate));
    }
}
$sumDiff = $compareDate->diff($initialDate);
var_dump($sumDiff);

Outputs:

object(DateInterval)#6 (15) {
  ["y"]=>
  int(312)
  ["m"]=>
  int(3)
  ["d"]=>
  int(12)
  ["h"]=>
  int(19)
  ["i"]=>
  int(53)
  ["s"]=>
  int(50)
  //...
}

Example: http://ideone.com/v9bzLe

Will B.
  • 17,883
  • 4
  • 67
  • 69
  • @joergi1988 updated to make the compare date more accurate - otherwise the `while` loop will cause the resulting `compareDate` to be off by the amount of time the `while` takes to complete. – Will B. May 30 '16 at 16:30