简体   繁体   中英

Calculate weekdays between two date

I want to calculate how many days in a Weekday. But it return "Uncaught Error: Call to a member function add() on integer".

Here my code

$dateStart_convert   = DateTime::createFromFormat("d/m/Y", $cuti_sdate);
$start = $dateStart_convert->getTimestamp();

$dateEnd_convert   = DateTime::createFromFormat("d/m/Y", $cuti_edate);
$end = $dateEnd_convert->getTimestamp();

$oneday = new DateInterval("P1D");

$workdays = array();

foreach(new DatePeriod($start, $oneday, $end->add($oneday)) as $day)
{
     $day_num = $day->format("N"); /* 'N' number days 1 (mon) to 7 (sun) */
     if($day_num < 6)
     { 
         $workdays[] = $day->format("Y-m-d");
     }
     $weekday_date = array_merge(array_diff($workdays, $cuti_date));

     $c_weekday = count($weekday_date);
}

use this:)

<?php 
$start = new DateTime($startDate);
$end = new DateTime($endDate);
$oneday = new DateInterval("P1D");
$days = array();
$data = "7.5";

/* Iterate from $start up to $end+1 day, one day in each iteration. We add one day to the $end date, because the DatePeriod only iterates up to, not including, the end date. */

foreach(new DatePeriod($start, $oneday, $end->add($oneday)) as $day) {
  $day_num = $day->format("N"); /* 'N' number days 1 (mon) to 7 (sun) */
  if($day_num < 6) { /* weekday */
    $days[$day->format("Y-m-d")] = $data;
  } 
}    

$weekdays = count($days);

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM