Jump to content

using for each with function


prince198

Recommended Posts

hello

 

i found class in php and i added some lines to calculate difference between 2 days by week

 

the function work perfet

so when i call my function like this

 


<?php
$dates = new Days;


print_r($dates->days_diff('2008-01-01', '2008-01-12'));

?>

i have resultat

 

week1: 4 days
week2: 5 days

 

weekend not including  :D

 

but now  if i want to calcultate from data base with many lines

 

start_date    end_date

2008-01-01  /  2008-01-05

2008-01-01  /  2008-02-05

 

i can't found solution  ???

i use foreach but i failed cause i have result evry line alone

 

week1: 4 days
week2: 5 days

week1: 4 days
week2: 5 days
week3: 5 days
week4: 5 days
week5: 5 days

...

 

there is other function  to calultate business days. to unterstand my problem

http://fr3.php.net/date

thank for help  :)

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/120213-using-for-each-with-function/
Share on other sites

this is function


<?php
function date_difference($start_date=array(), $end_date=array(), $workdays_only = true, $skip_holidays = true){
    $start_date = strtotime($start_date);
      $end_date = strtotime($end_date);
     $seconds_in_a_day = 86400;
       $sunday_val = "6";
       $saturday_val = "7";
       $workday_counter = 0;
        $holiday_array = array();
    
       $ptr_year = intval(date("Y", $start_date));
        $holiday_array[$ptr_year] = get_holidays(date("Y", $start_date));
    $day_val=array();
       for($day_val = $start_date; $day_val <= $end_date; $day_val+=$seconds_in_a_day){
           $pointer_day = date("N", $day_val);
          if($workdays_only == true){
              if(($pointer_day != $sunday_val) AND ($pointer_day != $saturday_val)){
                  if($skip_holidays == true){
                      if(intval(date("Y", $day_val))!=$ptr_year){
                          $ptr_year = intval(date("Y", $day_val));
                          $holiday_array[$ptr_year] = get_holidays(date("Y", $day_val));
                      }
                       if(!in_array($day_val, $holiday_array[date("Y", $day_val)])){
                          $workday_counter++;
                       }
                   }else{
                      $workday_counter++;
                   }
               }
           }else{
               if($skip_holidays == true){
                    if(intval(date("Y", $day_val))!=$ptr_year){
                       $ptr_year = intval(date("Y", $day_val));
                      $holiday_array[$ptr_year] = get_holidays(date("Y", $day_val));
                   }
                  if(!in_array($day_val, $holiday_array[date("Y", $day_val)])){
                      $workday_counter++;
                    }
             }else{
                  $workday_counter++;
              }
          }
       }
       return $workday_counter;
   }

 

so iif i call this function

<?php echo date_difference('2006-12-01', '2006-12-31');?>

 

i wil have result :20days

but how i add many dates to this function :

*

 

i tried this one but no result :-[

<?php
function date_difference($tab=array(array('start_date'=>'$start_date','end_date'=>'$end_date')), $workdays_only = true, $skip_holidays = true){
}

$tab= array();
array_push($tab ,array('start_date' =>'2006-12-12','end_date'=>'2006-12-25'));



foreach($tabe as $x){
print_r(date_difference('$x[start_date]', '$x[end_date]'));

}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.