Search
 
SCRIPT & CODE EXAMPLE
 

PHP

get DAYS absent from working days from given date range


<?php

$imaginaryWorkDatesOfWorker1 = array(
    '01-02-2012', '02-02-2012', '03-02-2012', '06-02-2012', '07-02-2012', '08-02-2012',
    '09-02-2012', '14-02-2012', '15-02-2012', '16-02-2012', '17-02-2012', '20-02-2012',
    '21-02-2012', '22-02-2012', '27-02-2012', '28-02-2012', '29-02-2012'    
);

$leaveDays1 = getLeaveDays(2, 2012, $imaginaryWorkDatesOfWorker1);
displayWorkersLeaveDays($leaveDays1);



function getLeaveDays($month, $year, $arrDatesPresent=array()){
  $arrAllWorkDatesInMonth = getDatesInTheMonth($month, $year);

  $leaveDays = array_diff($arrAllWorkDatesInMonth, $arrDatesPresent);
  return $leaveDays;
}


function getDatesInTheMonth($month, $year, $includeWeekends=false, $format2Use='d-m-Y')    {
  $arrDatesInTheMonth = array();
  if (empty($format2Use)) $format2Use = 'm-d-Y';

  if (empty($month) || empty($year)){
    throw new Exception("Invalid parameters given.");
  }
  else{
    $fauxDate = mktime(0, 0, 0, $month, 1, $year);
    $numOfDaysInMonth = date('t', $fauxDate);

    if (!empty($numOfDaysInMonth)){
        for ($day = 1; $day <= $numOfDaysInMonth; $day++){

            $timeStamp = mktime(0, 0, 0, $month, $day, $year);
            $cdate = date($format2Use, $timeStamp);

            if ($includeWeekends){
                $arrDatesInTheMonth[] = $cdate;
            }
            else{
                if (!isWeekend($cdate)) { $arrDatesInTheMonth[] = $cdate; }
            }
        }
    }
  }

  return $arrDatesInTheMonth;
}

function isWeekend($date) {
  return (date('N', strtotime($date)) >= 7);
}


function isWeekend2($date) {
  $weekDay = date('w', strtotime($date));
  return ($weekDay == 0 || $weekDay == 6);
}

function printDates($arrDates){
  foreach ($arrDates as $key => $cdate) {
      $display = sprintf( '%s <br />', date('[l] - jS of F Y', strtotime($cdate)) );
      echo $display;
  }
}

function displayWorkersLeaveDays($leaveDays){
  echo '<div style="background-color:#CCC;margin:10px 0;">';
  echo '<div>Your Leave days are as follows: </div>';
  printDates($leaveDays);
  echo '</div>';
}
?>
Comment

PREVIOUS NEXT
Code Example
Php :: php counting number of chars excluding newlines 
Php :: do shortcode wordpress 
Php :: wordpress acf get checkbox options 
Php :: destroy php variable 
Php :: get current route laravel 
Php :: php string starts with 
Php :: preg_match number only in php 
Php :: php merge 2 arrays 
Php :: incorrect format parameter phpmyadmin xampp 
Php :: cart icon in woocommerce 
Php :: file get content using call api in php 
Php :: php remove class attribute 
Php :: select session php 
Php :: laravel human readable date 
Php :: pathtophp in ubuntu lampp 
Php :: add new column to table laravel 
Php :: phpunit filter 
Php :: php return loading message 
Php :: laravel checkbox terms and conditions 
Php :: regex phpstorm 
Php :: xendit callback 
Php :: php convert number to month 
Php :: how to remove additional sidebar in magento 2 using xml 
Php :: check string in php 
Php :: how to take input in php 
Php :: Laravel Unable to migrate or Make Seeds 
Php :: convert stdclass object to array php 
Php :: make model -mcr laravel 
Php :: add new column to existing table laravel 
Php :: php serialize 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =