Réduire mon code à répétition

Fermé
steve2206 Messages postés 95 Date d'inscription lundi 20 octobre 2003 Statut Membre Dernière intervention 30 octobre 2013 - 30 oct. 2013 à 06:07
a70m Messages postés 2763 Date d'inscription jeudi 21 mai 2009 Statut Contributeur Dernière intervention 2 février 2014 - 30 oct. 2013 à 12:34
Bonjour à tous,

J'ai un bout de code, ou plutôt un pavé lol, et je suis certain qu'il y a moyen de réduire ce code en quelques lignes tant il y a de répétitions. ^^

Comment réduire le code suivant svp, sachant que je l'ai coupé car il n'y a pas 10fois la même chose mais 20 fois en réalité?

/////////////////////////////////////////////////// 0
if(isset($_POST['heure0']))
$my_strh0=$_POST['heure0'];
else
$_POST['heure0'] = "00-00-00";
/////////////////////////////////////////////////// 1
if(isset($_POST['heure1']))
$my_strh1=$_POST['heure1'];
else
$_POST['heure1'] = "00-00-00";

if(!isset($a1))
$a1 = 0;
/////////////////////////////////////////////////// 2
if(isset($_POST['heure2']))
$my_strh2=$_POST['heure2'];
else
$_POST['heure2'] = "00-00-00";

if(!isset($a2))
$a2 = 0;
/////////////////////////////////////////////////// 3
if(isset($_POST['heure3']))
$my_strh3=$_POST['heure3'];
else
$_POST['heure3'] = "00-00-00";

if(!isset($a3))
$a3 = 0;
/////////////////////////////////////////////////// 4
if(isset($_POST['heure4']))
$my_strh4=$_POST['heure4'];
else
$_POST['heure4'] = "00-00-00";

if(!isset($a4))
$a4 = 0;
/////////////////////////////////////////////////// 5
if(isset($_POST['heure5']))
$my_strh5=$_POST['heure5'];
else
$_POST['heure5'] = "00-00-00";

if(!isset($a5))
$a5 = 0;
/////////////////////////////////////////////////// 6
if(isset($_POST['heure6']))
$my_strh6=$_POST['heure6'];
else
$_POST['heure6'] = "00-00-00";

if(!isset($a6))
$a6 = 0;
/////////////////////////////////////////////////// 7
if(isset($_POST['heure7']))
$my_strh7=$_POST['heure7'];
else
$_POST['heure7'] = "00-00-00";

if(!isset($a7))
$a7 = 0;
/////////////////////////////////////////////////// 8
if(isset($_POST['heure8']))
$my_strh8=$_POST['heure8'];
else
$_POST['heure8'] = "00-00-00";

if(!isset($a8))
$a8 = 0;
/////////////////////////////////////////////////// 9
if(isset($_POST['heure9']))
$my_strh9=$_POST['heure9'];
else
$_POST['heure9'] = "00-00-00";

if(!isset($a9))
$a9 = 0;
/////////////////////////////////////////////////// 10
if(isset($_POST['heure10']))
$my_strh10=$_POST['heure10'];
else
$_POST['heure10'] = "00-00-00";

if(!isset($a10))
$a10 = 0;
////////////////////////////////////////////////////

/* VARIABLES */                    
// Données en chiffres
$my_strh0=$_POST['heure0'];
$my_strh1=$_POST['heure1'];
$my_strh2=$_POST['heure2'];
$my_strh3=$_POST['heure3'];
$my_strh4=$_POST['heure4'];
$my_strh5=$_POST['heure5'];
$my_strh6=$_POST['heure6'];
$my_strh7=$_POST['heure7'];
$my_strh8=$_POST['heure8'];
$my_strh9=$_POST['heure9'];
$my_strh10=$_POST['heure10'];

//Conversion des heures en TimeStamp
$h0=strtotime($my_strh0);
$h1=strtotime($my_strh1);
$h2=strtotime($my_strh2);
$h3=strtotime($my_strh3);
$h4=strtotime($my_strh4);
$h5=strtotime($my_strh5);
$h6=strtotime($my_strh6);
$h7=strtotime($my_strh7);
$h8=strtotime($my_strh8);
$h9=strtotime($my_strh9);
$h10=strtotime($my_strh10);

//Si la deuxieme heure est bien plus tard que la premiere
if($h0 > $h1) {$a1=($h0-$h1); }
if($h0 > $h2) {$a2=($h0-$h2); }
if($h0 > $h3) {$a3=($h0-$h3); }
if($h0 > $h4) {$a4=($h0-$h4); }
if($h0 > $h5) {$a5=($h0-$h5); }
if($h0 > $h6) {$a6=($h0-$h6); }
if($h0 > $h7) {$a7=($h0-$h7); }
if($h0 > $h8) {$a8=($h0-$h8); }
if($h0 > $h9) {$a9=($h0-$h9); }
if($h0 > $h10) {$a10=($h0-$h10);}


//Si c'est la premiere heure qui est plus tard que la deuxieme
if($h1 > $h0) {$a1=86400-($h1-$h0); }
echo "Trajet".$my_strh1."";
@$date1 = getdate($h1);

if($h2 > $h0) {$a2=86400-($h2-$h0); }
echo "Trajet".$my_strh2."";
@$date2 = getdate($h2);

if($h3 > $h0) {$a3=86400-($h3-$h0); }
echo "Trajet".$my_strh3."";
@$date3 = getdate($h3);

if($h4 > $h0) {$a4=86400-($h4-$h0); }
echo "Trajet".$my_strh4."";
@$date4 = getdate($h4);

if($h5 > $h0) {$a5=86400-($h5-$h0); }
echo "Trajet".$my_strh5."";
@$date5 = getdate($h5);

if($h6 > $h0) {$a6=86400-($h6-$h0); }
echo "Trajet".$my_strh6."";
@$date6 = getdate($h6);

if($h7 > $h0) {$a7=86400-($h7-$h0); }
echo "Trajet".$my_strh7."";
@$date7 = getdate($h7);

if($h8 > $h0) {$a8=86400-($h8-$h0); }
echo "Trajet".$my_strh8."";
@$date8 = getdate($h8);

if($h9 > $h0) {$a9=86400-($h9-$h0); }
echo "Trajet".$my_strh9."";
@$date9 = getdate($h9);

if($h10 > $h0) {$a10=86400-($h10-$h0); }
echo "Trajet".$my_strh10."";
@$date10 = getdate($h10);


//Retrait d'une heure de decalage o_O
$a1=$a1-3600;
$a2=$a2-3600;
$a3=$a3-3600;
$a4=$a4-3600;
$a5=$a5-3600;
$a6=$a6-3600;
$a7=$a7-3600;
$a8=$a8-3600;
$a9=$a9-3600;
$a10=$a10-3600;

echo "Depart:";
//reconversion du timestamp en heure pr?format?e (ici H M)
echo strftime("%H:%M:%S",$a1);
echo strftime("%H:%M:%S",$a2);
echo strftime("%H:%M:%S",$a3);
echo strftime("%H:%M:%S",$a4);
echo strftime("%H:%M:%S",$a5);
echo strftime("%H:%M:%S",$a6);
echo strftime("%H:%M:%S",$a7);
echo strftime("%H:%M:%S",$a8);
echo strftime("%H:%M:%S",$a9);
echo strftime("%H:%M:%S",$a10);





--
A voir également:

1 réponse

a70m Messages postés 2763 Date d'inscription jeudi 21 mai 2009 Statut Contributeur Dernière intervention 2 février 2014 217
30 oct. 2013 à 12:34
Salut,

Tu peux utiliser un foreach

https://www.php.net/manual/fr/control-structures.foreach.php

A+
a70m
0