Rechercher : dans
Par :

Parametre array dans fonction

Dernière réponse le 19 aoû 2008 à 12:13:46 tigre198, le 19 aoû 2008 à 11:05:57 
 Signaler ce message aux modérateurs

Bonjour,

j'ai trouvé fonctions qui calculte nombre de jours total entre deux dates sans compter weekend

<?php
//The function returns the no. of business days between two dates and it skeeps the holidays
function getWorkingDays($startDate,$endDate,$holidays){
    //The total number of days between the two dates. We compute the no. of seconds and divide it to 60*60*24
    //We add one to inlude both dates in the interval.
    $days = (strtotime($endDate) - strtotime($startDate)) / 86400 + 1;

    $no_full_weeks = floor($days / 7);
    $no_remaining_days = fmod($days, 7);

    //It will return 1 if it's Monday,.. ,7 for Sunday
    $the_first_day_of_week = date("N",strtotime($startDate));
    $the_last_day_of_week = date("N",strtotime($endDate));

    //---->The two can be equal in leap years when february has 29 days, the equal sign is added here
    //In the first case the whole interval is within a week, in the second case the interval falls in two weeks.
    if ($the_first_day_of_week <= $the_last_day_of_week){
        if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) $no_remaining_days--;
        if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) $no_remaining_days--;
    }
    else{
        if ($the_first_day_of_week <= 6) $no_remaining_days--;
        //In the case when the interval falls in two weeks, there will be a Sunday for sure
        $no_remaining_days--;
    }

    //The no. of business days is: (number of weeks between the two dates) * (5 working days) + the remainder
//---->february in none leap years gave a remainder of 0 but still calculated weekends between first and last day, this is one way to fix it
   $workingDays = $no_full_weeks * 5;
    if ($no_remaining_days > 0 )
    {
      $workingDays += $no_remaining_days;
    }

    //We subtract the holidays
    foreach($holidays as $holiday){
        $time_stamp=strtotime($holiday);
        //If the holiday doesn't fall in weekend
        if (strtotime($startDate) <= $time_stamp && $time_stamp <= strtotime($endDate) && date("N",$time_stamp) != 6 && date("N",$time_stamp) != 7)
            $workingDays--;
    }

    return $workingDays;
}
?>

et si je met
<?php
$holidays=array("2006-12-25","2006-12-26","2007-01-01");

echo getWorkingDays("2006-12-22","2007-01-06",$holidays)
?>

j'aurais resultat 8
mais comment on fait pour donner beaucoup de dates a cete fonction et avoir resultat total
par exemple si je done deux foix ces deux dates j'aurais résultat 16
j'ai essayé de déclarer array dans fonction mais toujours erreur:
<?php
Parse error: parse error, unexpected T_VARIABLE, expecting ')' 
?>
Configuration: Windows 2000
Internet Explorer 6.0

Meilleures réponses pour « parametre array dans fonction » dans :
Bash - Les paramètres VoirIntroduction Les paramètres positionnels Exemple 1 Les paramètres spéciaux Exemple 2 Initialiser des paramètres - La commande "set" - Exemples - La commande "shift" - Exemple 3 Introduction Il est possible de fournir à un script,...
Les fonctions en C++ : surcharge et paramètres par défaut. VoirLes fonctions en C++ : surcharge et paramètres par défaut. 1. La surcharge de fonctions et méthodes de classes, dont le constructeur 2. Utilisation des paramètres par défaut En C++, une même fonction ou méthode de classe peut être...
Paramètres GPRS VoirSi vous possédez un téléphone mobile fonctionnant sous Windows Mobile (smartphone du type HTC, Qtek, etc.), voici les paramètres GPRS pour les trois principaux opérateurs français Orange Orange GSM Orange GPRS Orange MMS Orange...
PHP - Les fonctions VoirLa notion de fonction On appelle fonction un sous-programme qui permet d'effectuer un ensemble d'instructions par simple appel de la fonction dans le corps du programme principal. Les fonctions permettent d'exécuter dans plusieurs parties du...
Javascript - Les fonctions VoirLa notion de fonction On appelle fonction un sous-programme qui permet d'effectuer un ensemble d'instructions par simple appel de la fonction dans le corps du programme principal. Cette notion de sous-programme est généralement appelée fonction...
Javascript - l'objet Array VoirLes particularités de l'objet Array L'objet Array est un objet du noyau Javascript permettant de créer et de manipuler des tableaux. Voici la syntaxe à utiliser pour créer une variable tableau : var x = new Array(element1[, element2, ...]); Si...

1

Tiller, le 19 aoû 2008 à 11:39:28

echo getWorkingDays("2006-12-22","2007-01-06",$holidays)

Il manque un point virgule a la fin

mais comment on fait pour donner beaucoup de dates a cete fonction et avoir resultat total
par exemple si je done deux foix ces deux dates j'aurais résultat 16 

J'ai pas trop compris, mais tu peux faire:
$dt = getWorkingDays("2006-12-22","2007-01-06",$holidays);
$dt += getWorkingDays("2007-12-22","2008-01-06",$holidays);

On dit que les oiseaux sont libres dans le ciel,
Mais la vraie liberté n'est pas celle d'avoir un endroit
où se poser ?

Répondre à Tiller

2

tigre198, le 19 aoû 2008 à 11:50:50

Merçi de me répondre
en faite je veux partir d'une table my sql ou j'ai des milliers de lignes date_debut et dat_fin

par exemple

2008-01-01- / 2008-01-05
2008-01-01 / 2008-01-07

et donc grace a cette fonction je peux avoir comme résultat

nombre de jour total = 12;

j'ai essayé avec boucle for each avec des array mais sans résultat
j'espere t'a compris probleme;

sinon cool ton proverbe !!!!!

Répondre à tigre198

3

Tiller, le 19 aoû 2008 à 11:54:50
  • +1

<?php
// Connexion SQL
[...]

$nombreTotal = 0;
$holidays = array("2006-12-25","2006-12-26","2007-01-01");

$sql = 'SELECT * FROM `taTable`';
$req = mysql_query($sql);
while ($data = mysql_fetch_array($req))
{
    $nombreTotal += getWorkingDays($data['debut'], $data['fin'], $holidays);
}

?>
On dit que les oiseaux sont libres dans le ciel,
Mais la vraie liberté n'est pas celle d'avoir un endroit
où se poser ?

Répondre à Tiller

4

 tigre198, le 19 aoû 2008 à 12:13:46

Merçi pour ta réponse
ça marche !!!
juste reste temps execution important

pour 1000 ligne ça prend déja deux secondes

je vais essayer avec systeme cache
merçi encore une fois

Répondre à tigre198