<?php
/*
 * This file is part of Web Pro Manager.
 * 
 * Web Pro Manager is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Web Pro Manager is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Web Pro Manager.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL
 * @package WebProManager
*/

function nGetDateDiffDays($nDateToCompare){
	// Accepts date in YYYY-MM-DD format
	$nDate = date("Y-m-d 0:0:0");
	$nDateToCompare = $nDateToCompare." 0:0:0";
	$nDateDiff = round((strtotime($nDate) - strtotime($nDateToCompare))/60/60/24);
	return $nDateDiff;
}
function nDateToDisplayCustom($nDate,$nFormat){
	return date($nFormat,strtotime($nDate));
}
function nDateToDisplay($nDate){
	if(!$nDate){ return ""; }
	// Accepts date in YYYY-MM-DD HH:MM:SS format, and converts to MM/DD/YYYY
	$nDateTimeARRAY = explode(" ",$nDate);
	$nDateARRAY = explode("-",$nDateTimeARRAY[0]);
	return $nDateARRAY[1]."/".$nDateARRAY[2]."/".$nDateARRAY[0];
}
function nDateToMySQL($nDate){
	// Accepts date in MM/DD/YYYY format, and converts to YYYY-MM-DD
	$nDateARRAY = explode("/",$nDate);
	return $nDateARRAY[2]."-".$nDateARRAY[0]."-".$nDateARRAY[1];
}
function nGetFirstOfMonth($nDate){
	if(!$nDate){ return ""; }
	// accepts and returns date in YYYY-MM-DD or YYYY-MM-DD HH:MM:SS format only
	$nDateTimeARRAY = explode(" ",$nDate);
	$nDateARRAY = explode("-",$nDateTimeARRAY[0]);
	$nMonth = $nDateARRAY[1];
	// add leading zero to month if needed
	if(strlen($nMonth)==1){ $nMonth="0".$nMonth; }
	return $nDateARRAY[0]."-".$nMonth."-01";
}
function nGetDay($nDate,$nMovement=0){
	if(!$nDate){ return ""; }
	// accepts and returns date in YYYY-MM-DD or YYYY-MM-DD HH:MM:SS format only
	$nNewDate = strtotime ("$nMovement day", strtotime ( $nDate ) ) ;
	$nNewDate = date ( 'Y-m-j' , $nNewDate );
	return $nNewDate;
}
function nGetMonth($nDate,$nMovement=0){
	if(!$nDate){ return ""; }
	// accepts and returns date in YYYY-MM-DD or YYYY-MM-DD HH:MM:SS format only
	$nDateTimeARRAY = explode(" ",$nDate);
	$nDateARRAY = explode("-",$nDateTimeARRAY[0]);
	$nDay = $nDateARRAY[2];
	$nMonth = $nDateARRAY[1];
	$nYear = $nDateARRAY[0];
	$nMonth = $nMonth + $nMovement;
	while($nMonth>12){ $nYear++; $nMonth=$nMonth-12; }
	while($nMonth<1){ $nYear--; $nMonth=$nMonth+12; }
	if(cal_days_in_month(CAL_GREGORIAN,$nMonth,$nYear)<$nDay){ $nDay = cal_days_in_month(CAL_GREGORIAN,$nMonth,$nYear); }
	// add leading zero to month if needed
	if(strlen($nMonth)==1){ $nMonth="0".$nMonth; }
	return "$nYear-$nMonth-$nDay";
}
function nGetYear($nDate,$nMovement=0){
	if(!$nDate){ return ""; }
	// accepts and returns date in YYYY-MM-DD or YYYY-MM-DD HH:MM:SS format only
	$nDateTimeARRAY = explode(" ",$nDate);
	$nDateARRAY = explode("-",$nDateTimeARRAY[0]);
	$nYear = $nDateARRAY[0];
	$nYear = $nYear + $nMovement;
	return $nYear;
}
function nGetMonthName($nDate){
	if(!$nDate){ return ""; }
	$nDateTimeARRAY = explode(" ",$nDate);
	$nDateARRAY = explode("-",$nDateTimeARRAY[0]);
	$nMonth = $nDateARRAY[1];
	switch($nMonth){
		case "1": $nMonthName="January"; break;
		case "2": $nMonthName="February"; break;
		case "3": $nMonthName="March"; break;
		case "4": $nMonthName="April"; break;
		case "5": $nMonthName="May"; break;
		case "6": $nMonthName="June"; break;
		case "7": $nMonthName="July"; break;
		case "8": $nMonthName="August"; break;
		case "9": $nMonthName="September"; break;
		case "10": $nMonthName="October"; break;
		case "11": $nMonthName="November"; break;
		case "12": $nMonthName="December"; break;
	}
	return $nMonthName;
}

function dateDiff($dateTimeBegin,$dateTimeEnd,$itemToReturn="d") {
	$dateTimeBegin =strtotime($dateTimeBegin);
	$dateTimeEnd  =strtotime($dateTimeEnd);
	
	if($dateTimeEnd === -1 || $dateTimeBegin === -1) {
	 # error condition
	 return false;
	}
	
	$diff = $dateTimeEnd - $dateTimeBegin;
	
	if ($diff < 0) {
	 # error condition
	 return false;
	}
	
	$weeks = $days = $hours = $minutes = $seconds = 0; # initialize vars
	
	if($diff % 604800 > 0) {
	 $rest1  = $diff % 604800;
	 $weeks  = ($diff - $rest1) / 604800; # seconds a week
	 if($rest1 % 86400 > 0) {
	   $rest2 = ($rest1 % 86400);
	   $days  = ($rest1 - $rest2) / 86400; # seconds a day
	   if( $rest2 % 3600 > 0 ) {
	     $rest3 = ($rest2 % 3600);
	     $hours = ($rest2 - $rest3) / 3600; # seconds an hour
	     if( $rest3 % 60 > 0 ) {
	       $seconds = ($rest3 % 60);
	       $minutes = ($rest3 - $seconds) / 60;  # seconds a minute
	     } else {
	       $minutes = $rest3 / 60;
	     }
	   } else {
	     $hours = $rest2 / 3600;
	   }
	 } else {
	   $days = $rest1/ 86400;
	 }
	}else {
	 $weeks = $diff / 604800;
	}
	
	$string = array();
	if($weeks > 1) {
	 $string[]  = "$weeks weeks";
	} elseif ($weeks == 1) {
	 $string[]  = "a week";
	}
	if($days > 1) {
	 $string[] = "$days days";
	} elseif($days == 1) {
	 $string[] = "a day";
	}
	if($hours > 1) {
	 $string[] = "$hours hours";
	} elseif ($hours == 1) {
	 $string[] = "an hour";
	}
	if($minutes > 1) {
	 $string[] = "$minutes minutes";
	} elseif ($minutes == 1) {
	 $string[] = "a minute";
	}
	if($seconds > 1) {
	 $string[] = "$seconds seconds";
	} elseif($seconds == 1) {
	 $string[] = "a second";
	}
	
	# join together all the strings in the array above except the last element
	$text  = join(', ', array_slice($string,0,sizeof($string)-1)) . ", and ";
	$text .= array_pop($string);  # put the last one on after the and
	
	switch($itemToReturn){
		case "d":
			return ($weeks*7)+$days;
			break;
	}
	
	//return array($text, $weeks, $days, $hours, $minutes, $seconds);
}
function nGetEndDate($nTimeInPast,$nTimeForward,$nPeriod,$nReturnStartDate=0){
	// Gets start/end dates based on passed-in vars
	if($nPeriod=="m"){
		// Months
		$nStartDate = date("Y-m-d H:m:s",mktime(0, 0, 0, date("m")-$nTimeInPast, date("d"),  date("Y")));
		$nStartDate = nGetFirstOfMonth($nStartDate);
		if($nTimeForward==0){
			$nEndDate = date("Y-m-d H:m:s");
		} else {
			$nEndDate = date("Y-m-d H:m:s",mktime(0, 0, 0, date("m",strtotime($nStartDate))+$nTimeForward, date("d",strtotime($nStartDate)),  date("Y",strtotime($nStartDate))));
		}
	} else {
		// Days
		$nStartDate = date("Y-m-d H:m:s",mktime(0, 0, 0, date("m"), date("d")-$nTimeInPast,  date("Y")));
		if($nTimeForward==0){
			$nEndDate = date("Y-m-d H:m:s");
		} else {
			$nEndDate = date("Y-m-d H:m:s",mktime(0, 0, 0, date("m",strtotime($nStartDate)), date("d",strtotime($nStartDate))+$nTimeForward,  date("Y",strtotime($nStartDate))));
		}
	}
	if($nReturnStartDate){ return $nStartDate; }
	return $nEndDate;
}
?>