<?php 
function daysLeftForday($devadate) 
{ 
 
    /* input day date format -> Y-m-d */ 
    list($y, $m, $d) = explode('-',$devadate); 
     $nowdate = mktime(0, 0, 0, date("m"), date("d"), date("Y")); 
    $nextday = mktime(0,0,0,$m, $d, date("Y")); 

    if ($nextday<$nowdate) 
        $nextday=$nextday+(60*60*24); 

    $daycount=intval((-$nextday+$nowdate)/(60*60*24)); 

    return $daycount; 
} 

function daysLeftForday_payble($date1, $date2) { 
    $current = $date1; 
    $datetime2 = date_create($date2); 
    $count = 0; 
    while(date_create($current) < $datetime2){ 
        $current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current))); 
        $count++; 
    } 
    return $count; 
} 

/*function daysLeftForday_payble($startDate, $endDate)
        {

            $date1=date_create($startDate);
            $date2=date_create($endDate);
            $diff=date_diff($date1,$date2);
            $daycount= $diff->format("%a");
            return $daycount;
        } 
*/
 

function GetMonth($month)
{
switch($month)
	   {
		  case 1:$m='Jan'   ;break;
		  case 2:$m='Feb'   ;break;
		  case 3:$m='March'   ;break;
		  case 4:$m='Apr'   ;break;
		  case 5:$m='May'   ;break;
		  case 6:$m='June'   ;break;
		  case 7:$m='July'   ;break;
		  case 8:$m='Aug'   ;break;
		  case 9:$m='Sept'   ;break;
		  case 10:$m='Oct'   ;break;
		  case 11:$m='Nov'   ;break;
		  case 12:$m='Dec'   ;break;
		}
		return($m);
}

function GetDay($day)
{
 switch($day)
	   {
		  case 1:$m='Mon'   ;break;
		  case 2:$m='Tue'   ;break;
		  case 3:$m='Wed'   ;break;
		  case 4:$m='Thu'   ;break;
		  case 5:$m='Fri'   ;break;
		  case 6:$m='Sat'   ;break;
		  case 7:$m='Sun'   ;break;
		 
		}
		return($m);
}

function number_to_words ($x)
 {
 	 
	  $nwords = array("","one","two","three","four","five","six", 
                  "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", 
                  "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", 
                  "nineteen", "twenty", 30 => "thirty", 40 => "fourty",
                     50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eigthy",
                     90 => "ninety" );

     if(!is_numeric($x))
     {
         $w = '#';
     }else if(fmod($x, 1) != 0)
     {
         $w = '#';
     }else{
         if($x < 0)
         {
             $w = 'minus ';
             $x = -$x;
         }else{
             $w = '';
         }
         if($x < 21)
         {
             $w .= $nwords[$x];
         }else if($x < 100)
         {
             $w .= $nwords[10 * floor($x/10)];
             $r = fmod($x, 10);
             if($r > 0)
             {
                 $w .= ' '. $nwords[$r];
             }
         } else if($x < 1000)
         {
                
                $w .= $nwords[floor($x/100)] .' hundred';
             $r = fmod($x, 100);
             if($r > 0)
             {
                 $w .= ' '. number_to_words($r);
             }
         } else if($x < 1000000)
         {
                $w .= number_to_words(floor($x/1000)) .' thousand';
             $r = fmod($x, 1000);
             if($r > 0)
             {
                 $w .= ' ';
                 if($r < 100)
                 {
                     $w .= ' ';
                 }
                 $w .= number_to_words($r);
             }
         } else {
             $w .= number_to_words(floor($x/1000000)) .' million';
             $r = fmod($x, 1000000);
             if($r > 0)
             {
                 $w .= ' ';
                 if($r < 100)
                 {
                     $word .= ' ';
                 }
                 $w .= number_to_words($r);
             }
         }
     }
     return $w;}


// find first day of month
function firstOfMonth( $m,$y) {
return date("Y-m-d", strtotime(date($m).'/01/'.date($y).' 00:00:00'));
}

// find Last day of month
function lastOfMonth( $m,$y) {
return date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date($m).'/01/'.date($y).' 00:00:00'))));
}

// COUNT  SUN
function CountSatSun($m,$y,$emp_weekly_off)
 {
  	    $s1=firstOfMonth($m,$y);
        $e1=lastOfMonth( $m,$y);
 	$c1=0;
 			for( $k=strtotime($s1);$k<=strtotime($e1);$k=$k+86400)
			{
							 $dx = strtotime ( '+0 day' ,$k) ;
							 $temp = date ( 'Y-m-d' , $dx);
							 $d1=explode('-',$temp);
							   $day=date('D',mktime(0,0,0,$d1[1],$d1[2],$d1[0]));
							//echo "<br>";
							 if( $day== GetDay($emp_weekly_off) ){
							 $c1++;
							}
			}
			return($c1);
}			
 //==========================
 function DayOfDate($startdate)
{
	$k=strtotime($startdate );
	$dx = strtotime ( '+0 day' ,$k) ;
 	$temp = date ( 'Y-m-d' , $dx);
 	$d1=explode('-',$temp);
    $day=date('D',mktime(0,0,0,$d1[1],$d1[2],$d1[0]));
	return($day);
}			


function GET_Holidays($day,$month, $years )
{
	global $db;
      $GetRecord = "Select * from  holidays where  day(start_date)='".$day."' and  month(start_date)='".$month."' and published='Y' and  year(start_date)='".$years."' ";	   	   
       $ResultData = DB_query($GetRecord,$db);
	   // DB_num_rows($ResultData);
   	  $RowData = DB_fetch_array($ResultData);
	  
	  if(DB_num_rows($ResultData)>0)
	     return(1);
		 else 
		 return(0);
}


function GET_Shift_hours($emp_shift)
{
	global $db;
     $GetRecord = "Select shift_start  ,start_AMPM , shift_end , end_AMPM from  shift_master where  id='".$emp_shift."'  ";	 
	  $ResultData = DB_query($GetRecord,$db);
	  // echo DB_num_rows($ResultData);
   	  $RowData = DB_fetch_array($ResultData);
	 $time1= strtotime($RowData['shift_start'].' '.$RowData['start_AMPM']);
	 $time2=strtotime($RowData['shift_end'].' '.$RowData['end_AMPM']);
   	   $delta_T = ($time2 - $time1);
//echo date("h:i:s",$delta_T);
// echo '===='.$delta_T .'====<br>=='.date("h:i ",$delta_T);
  return 8;

	  return date("h:i ",$delta_T);
	  
}


function getTimeDiff($dtime,$atime){
 
 $nextDay=$dtime>$atime?1:0;
 $dep=explode(':',$dtime);
 $arr=explode(':',$atime);
 $diff=abs(mktime($dep[0],$dep[1],0,date('n'),date('j'),date('y'))-mktime($arr[0],$arr[1],0,date('n'),date('j')+$nextDay,date('y')));
 $hours=floor($diff/(60*60));
 $mins=floor(($diff-($hours*60*60))/(60));
 $secs=floor(($diff-(($hours*60*60)+($mins*60))));
 if(strlen($hours)<2){$hours="0".$hours;}
 if(strlen($mins)<2){$mins="0".$mins;}
 if(strlen($secs)<2){$secs="0".$secs;}
 return $hours.':'.$mins;

// return $hours.':'.$mins.':'.$secs;
}
 
 
/* 
 
 function date_parse_from_format($format, $date) {
  $dMask = array(
    'H'=>'hour',
    'i'=>'minute',
    's'=>'second',
    'y'=>'year',
    'm'=>'month',
    'd'=>'day'
  );
  $format = preg_split('//', $format, -1, PREG_SPLIT_NO_EMPTY); 
  $date = preg_split('//', $date, -1, PREG_SPLIT_NO_EMPTY); 
  foreach ($date as $k => $v) {
    if ($dMask[$format[$k]]) $dt[$dMask[$format[$k]]] .= $v;
  }
  return $dt;
}
 */
function getMonths($start, $end) {
     
	 $startParsed = date_parse_from_format('Y-m-d', $start);
    $startMonth = $startParsed['month'];
    $startYear = $startParsed['year'];

    $endParsed = date_parse_from_format('Y-m-d', $end);
    $endMonth = $endParsed['month'];
    $endYear = $endParsed['year'];

    return ($endYear - $startYear) * 12 + ($endMonth - $startMonth) + 1;
}


function dateRange($first, $last, $step = '+1 day', $format = 'Y-m-d' ) { 

    $dates = array();
    $current = strtotime($first);
    $last = strtotime($last);

    while( $current <= $last ) { 

        $dates[] = date($format, $current);
        $current = strtotime($step, $current);
    }

    return $dates;
}


function multi_implode($array, $glue) {
    $ret = '';

    foreach ($array as $item) {
        if (is_array($item)) {
            $ret .= multi_implode($item, $glue) . $glue;
        } else {
            $ret .= $item . $glue;
        }
    }

    $ret = substr($ret, 0, 0-strlen($glue));

    return $ret;
}
 
 
 // GET MAC ADDRESS
 
 //Calculate Week date
function getWeek($week, $year) { 
  $dto = new DateTime();
  $result['start'] = $dto->setISODate($year, $week, 0)->format('Y-m-d');
  $result['end'] = $dto->setISODate($year, $week, 6)->format('Y-m-d');
  return $result;
}
	 ?>