Sindbad~EG File Manager

Current Path : /usr/home/beeson/public_html/helpwithmath/
Upload File :
Current File : /usr/home/beeson/public_html/helpwithmath/dates.php

<?php
// 1.17.11  changed date_selector to go 8 years into the future rather than 6 years to accomodate a 2018 gift card.
// 10.10.14 added monthNumber
function SqlDate($today)
// convert a PHP date object to a string in yyyy-mm-dd format
{  $month = $today['mon'];
   if($month < 10)
       $month = "0" . $month;
   $day = $today['mday'];
   if($day < 10)
      $day = "0" . $day;
   return $today['year'] . "-" .$month . "-" . $day;
}
function format_date($date)
   // convert a PHP date array to a string for display
{  return $date['month'] . " " . $date['mday'] . ", " . $date['year'];
}

function leap_year($y)
// good for the next 3300 years or so
{  if($y % 400 == 0)
       return true;
   if($y % 100 == 0)
      return false;
   if($y % 4 == 0)
      return true;
   return false;	  
}   

function mlength($n,$y)
// return number of days in month n in year $y
{ if($n == 9 || $n == 4 || $n == 6 || $n == 11)
     return 30;
  if($n == 2)
    { if(leap_year($y)) 
	     return 29;
	  else 
	     return 28;
	}
  return 31;
}  

global $daynames, $monthnames, $monthnumbers;
$daynames = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$monthnames = array('dummy','January','February','March', 'April','May','June','July','August','September','October','November','December');
$monthnumbers = array('January' => 1,'February' => 2,'March' => 3, 'April' => 4,
   'May' => 5,'June' => 6,'July' => 7,'August' => 8,'September' => 9,'October' => 10,'November' => 11,'December' => 12);
   
function add_months($date, $n)
// add $n months to the specified PHP date.   $n should be an integer and will anyway be truncated if it isn't 
// the 'weekday' field of the return value will often be wrong, so don't use it.
{ global $monthnames; 
  $ans = $date;
  $n = (int) $n; 
  $years = (int)( $n / 12);
  if($years > 0)
      { $ans['year'] += $years;
	    $n = $n % 12;
	  }
  $ans['mon'] += $n;
  if($ans['mon'] > 12)
      { $ans['year'] ++;
	    $ans['mon'] -= 12;
	  }
  // now we could have Feb. 31 for example 
  if($ans['mday'] > mlength($ans['mday'],$ans['year']))
      { $ans['mon'] = ($ans['mon'] + 1 ) % 12;
	    $ans['mday'] = mlength($ans['mday'], $ans['year']) - $ans['mon'];
	  }
  $ans['month'] = $monthnames[$ans['mon']];
  return $ans;
 }
	   
 
  
function add_days($date, $n)
// add n days to the specified PHP date. $n is at most 25 so we never overflow more than into the next month.
// adjust year, yday, weekday, month, wday, mon, mday fields correctly
{ global $daynames; 
  $ans = $date;
   $newday = $date['mday'] + n;
   $mlen = mlength($date['mon'],$date['year']);
   if($newday > $mlen)
      { // overflow into next month
	    if($date['mon'] == 12)
		   { // overflow into next year
		     $ans['year'] = $date['year'] + 1;
			 $ans['mon'] = 1;  // January			
		   }
		else  // same year
		   { $ans['mon'] = $date['mon'] + 1;
		   }
		 $ans['mday'] = $newday-$mlen + 1;     
	  }
   else  // now we're staying in the same month
      { $ans['mday'] = $newday;
	  }
   $ans['yday'] = $date['yday'] + $n;
   $ans['wday'] = ($date['wday'] + $n) % 7;
   $ans['weekday'] = $daynames[$ans['wday']];   
   return $ans;
}    

function holiday($date)
// return true if $date is a holiday
{ if($date['mon'] == 12 && $date['mday'] == 25)
     return true;  // Christmas
  if($date['mon'] == 1 && $date['mday'] == 1) 
     return true;  // New Year's
  return false;
}  	 

function next_business_day($date)
{ if($date['wday'] == 0) // Sunday
      $x = add_days($date,1);	
  else if($date['wday'] == 6)  // Saturday
      $x = add_days($date,2);
  else if($date['wday'] == 5) // Friday
      $x = add_days($date,3);
  else 
      $x = add_days($date,1);
  if(holiday($x))
      return next_business_day($x);
  return add_days($x,1);
} 

function date_selector($MonthField, $YearField,$SelectedMonth,$SelectedYear)
// offer user a foolproof way of selecting an expiration date
// they must select, rather than enter, a month and year.  Of course,
// they can still select a date in the past; this is checked later.
{  echo "<select name='$MonthField'> ";
   global $monthnames;
   for($i=1;$i<=12;$i++)
       { $month = $monthnames[$i];
	     if($month == $SelectedMonth)
		  { echo "<option selected value='$month' >$month </option>";
			}
		 else
 	        { echo "<option value='$month'>$month </option>";
			}
	   }
   echo "</select>";
   echo " <select name='$YearField'>";
   $thisyear = date("Y");
   for($i=0;$i<=8;$i++)
      { $year = $thisyear + $i;
	    if($year == $SelectedYear)
           { echo "<option  value='$year' selected>$year</option> ";
		   }

		else
	       { echo "<option  value='$year'>$year</option> ";
		   }
      }
   echo "</select> ";	  
}
function formatExpirationDate($month,$year)
// $month is spelled out;  return $mmyyyy format
{ global $monthnumbers;
  if($monthnumbers[$month] < 10)
     { return "0" . $monthnumbers[$month] . $year; 
	 }
  return $monthnumbers[$month] . $year;
} 

function monthNumber($month)
// return a number 1 to 12,  for a valid $month 
{ global $monthnumbers; 
  $d = $monthnumbers[$month];    
  if($d < 10) 
     return "0" . $d;
  return $d;
}

function validateExpirationDate($month, $year)
// return "true" or an error message
{ $thismonth = date("n");  // numerical month without leading zeroes
  $thisyear = date("Y");  // four digit numerical year  
  global $monthnames;
  for($i=1;$i<=12;$i++)
     { if($monthnames[$i] == $month)
	      { $monthNumber = $i;
		    break;
		  }
	 }
  if($i==13)
     return "Invalid expiration month.";		  
  if(($year == $thisyear && $monthNumber < $thismonth) || $year < $thisyear)
    return "Expiration date given is in the past.";
  return "true";
}  	
  	  
?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists