Bonjour
Je demande votre aide pour debloquer une situation.
J'essai de calculer l'ecart en heures entre deux dates.


J'arrive à sélectionner les données depuis la bdd et les afficher en array.
Ce que je souhaiterais c'est sortir le tableau avec nom - prenom - ecart.


Pour le calcul j'utilise une fonction qui le fait bien, cependant je ne sais pas comment associer ceci à ma requête pour sortir le tableau comme je souhaite.

Je demande votre aide


Ma requête

Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
include('db.php');
 
	$statement = $connection->prepare("SELECT nom, prenom, datedebut, datefin FROM tbl_test");
	$statement->execute();
	$result = $statement->fetchAll();
	$data = array();
	foreach ($result as $row) {
		$data[] = $row;	
	}
 
	echo json_encode($data);

La fonction qui calcul l'écart
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function biss_hours($start, $end){
 
    $startDate = new DateTime($start);
    $endDate = new DateTime($end);
    $periodInterval = new DateInterval( "PT1H" );
 
    $period = new DatePeriod( $startDate, $periodInterval, $endDate );
    $count = 0;
 
    foreach($period as $date){
 
    $startofday = clone $date;
    $startofday->setTime(8,30);
 
    $endofday = clone $date;
    $endofday->setTime(17,30);
 
        if($date > $startofday && $date <= $endofday && !in_array($date->format('l'), array('Sunday','Saturday'))){
 
            $count++;
        }
 
    }
 
	//Get seconds of Start time
	$start_d = date("Y-m-d H:00:00", strtotime($start));
	$start_d_seconds = strtotime($start_d);
	$start_t_seconds = strtotime($start);
	$start_seconds = $start_t_seconds - $start_d_seconds;
 
	//Get seconds of End time
	$end_d = date("Y-m-d H:00:00", strtotime($end));
	$end_d_seconds = strtotime($end_d);
	$end_t_seconds = strtotime($end);
	$end_seconds = $end_t_seconds - $end_d_seconds;
 
	$diff = $end_seconds-$start_seconds;
 
	if($diff!=0):
		$count--;
	endif;
 
	$total_min_sec = date('i:s',$diff);
 
	return $count .":".$total_min_sec;
}


La je fais le calcul avec ca

Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
$start = $row["datedebut"];
$end = $row["datefin"];
 
$ecart = biss_hours($start,$end);


Merci d'avance pour votre aide