PHP : CDR stuff

// 28, 29, 30

	$fDate = '12/02/28';
	$fTime = '20:30';
	$tDate = '12/03/01';
	$tTime = '21:30';
	
	$from = getYMDHm($fDate, $fTime);		
	$fromTimeStamp = mktime($from['hour'], $from['minute'], $from['second'], $from['month'], $from['day'], $from['year']);
	$to = getYMDHm($tDate, $tTime);
	$toTimeStamp = mktime($to['hour'], $to['minute'], $to['second'], $to['month'], $to['day'], $to['year']);

	$_30min = array('timeUnit' => 48, 'frequency' => 1800); // 24 * 2, 60 * 30
	$_60min = array('timeUnit' => 24, 'frequency' => 3600); // 24 * 1, 60 * 30 * 2
	$_1440min = array('timeUnit' => 1, 'frequency' => 86400); // 24 * (1 / 24), 60 * 60 * 24
	
	//$cdrs = getCdrs30Minutes($fromTimeStamp, $toTimeStamp);
	//$cdrs = getCdrFileNames($fromTimeStamp, $toTimeStamp, (24 * 2), (60 * 30));
	$cdrs = getCdrFileNames($fromTimeStamp, $toTimeStamp, $_1440min['timeUnit'], $_1440min['frequency']);
	foreach ($cdrs as $cdr) {
		echo '<br />' . $cdr;
	}
	
	echo '<br />done';
	//$fromDate = date('Y/m/d H:i:s', $fromTimeStamp);	
	//$toDate = date('Y/m/d H:i:s', $toTimeStamp);	
	//echo $fromDate . '<br />';
	//echo $toDate . '<br />';
	
	function getCdrs30Minutes($from, $to) {
		$numUnits = round(($to - $from) / 86400 * 24 * 2) + 1;
		//echo '<br />' . date('Y/m/d H:i:s', $from);
		//echo '<br />' . date('Y/m/d H:i:s', $to);
		$cdrs = array();        
		for ($i = 0; $i < $numUnits; $i++) {
			$cdr = date('Y/m/d H:i:s', ($from + ($i * 60 * 30)));
			$date_time = explode(" ", $cdr);
			$date = explode("/", $date_time[0]);
			$time = explode(":", $date_time[1]);
			$cdrs[] = substr($date[0], -2) . $date[1] . $date[2] . $time[0] . $time[1];
			//echo $cdr;
		}        
		return $cdrs;
	}
	
	function getCdrs60Minutes($from, $to) {
		$numUnits = round(($to - $from) / 86400 * 24) + 1;
		//echo '<br />' . date('Y/m/d H:i:s', $from);
		//echo '<br />' . date('Y/m/d H:i:s', $to);
		$cdrs = array();        
		for ($i = 0; $i < $numUnits; $i++) {
			$cdr = date('Y/m/d H:i:s', ($from + ($i * 60 * 60)));
			$date_time = explode(" ", $cdr);
			$date = explode("/", $date_time[0]);
			$time = explode(":", $date_time[1]);
			//$cdrs[] = substr($date[0], -2) . $date[1] . $date[2] . $time[0] . $time[1];
			$cdrs[] = substr($date[0], -2) . $date[1] . $date[2] . $time[0] . '*';
		}        
		return $cdrs;
	}
	
	function getCdrs1440Minutes($from, $to) {
		$numUnits = round(($to - $from) / 86400) + 1;
		//echo '<br />' . date('Y/m/d H:i:s', $from);
		//echo '<br />' . date('Y/m/d H:i:s', $to);
		$cdrs = array();        
		for ($i = 0; $i < $numUnits; $i++) {
			$cdr = date('Y/m/d H:i:s', ($from + ($i * 60 * 60 * 24)));
			$date_time = explode(" ", $cdr);
			$date = explode("/", $date_time[0]);
			$time = explode(":", $date_time[1]);
			//$cdrs[] = substr($date[0], -2) . $date[1] . $date[2] . $time[0] . $time[1];
			$cdrs[] = substr($date[0], -2) . $date[1] . $date[2] . '*';
		}        
		return $cdrs;
	}
	
	function getCdrFileNames($from, $to, $timeUnit, $frequency) {
		echo "$from, $to, $timeUnit, $frequency";
		$numUnits = round(($to - $from) / 86400  * $timeUnit) + 1;
		$cdrs = array();        
		for ($i = 0; $i < $numUnits; $i++) {
			$cdr = date('Y/m/d H:i:s', ($from + ($i * $frequency)));
			$date_time = explode(" ", $cdr);
			$date = explode("/", $date_time[0]);
			$time = explode(":", $date_time[1]);
			$cdrs[] = substr($date[0], -2) . $date[1] . $date[2] . $time[0] . $time[1];
			//echo $cdr;
		}        
		return $cdrs;
	}
	
	function getYMDHm($date_, $time_) {
		$date = explode('/', $date_);
		$time = explode(':', $time_);
		// TODO: fix 20
		return array('year' => '20' . $date[0], 'month' => $date[1], 'day' => $date[2], 'hour' => $time[0], 'minute' => $time[1], 'second' => 0);
	}
	
	function getCdr($cdr) {
		$date_time = explode(" ", $cdr);
		$date = explode("/", $date_time[0]);
		$time = explode(":", $date_time[1]);
		return substr($date[0], -2) . $date[1] . $date[2] . $time[0] . $time[1];
	}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: