<?php

/** function timeS2timeF
* returns a formatet time-string (like date)
* Inputs:
* 	$t    : 	the time
				So you give '12.5h'   this mean 12h+30m.
				So you give '12,5h'   this mean 12h+30m.
				So you give '12.5m'   this mean 12m+30s.
				So you give '12,5m'   this mean 12m+30s.
				So you give '12:30'   this mean 12h+30m.
				So you give '2:12:30' this mean 2h+12m+30s.
				So you give  '12' 	  this mean 12h.
				So you give '10m' 	  this mean mean 10m (600s).
				So you give '10.5m'   this mean mean 10m+30se (630s).				
				So you give '10,5m'   this mean mean 10m+30se (630s).				
				So you give '600s'    this mean 10min (600s).
				So you give '600.0s'  this mean 10min (600s).								
				So you give '600,0s'  this mean 10min (600s).				

				
*   $format : 	the output format (show http://php.net/manual/de/function.date.php)
				default: 'H:i'
				'U' give the seconds from 00:00:00
*   $round  : 	round to seconds ('seconds' and all unknown input) 
                   or to minutes ( 'm' ( maybe followed by chars ))
                   or to houres  ( 'h' ( maybe followed by chars ))
*   limit24   :  Begrenzen auf 24h? true, false
*   $errType : E_USER_ERROR or E_USER_NOTICE ( throwed if time > 24h or not detectable )
#**/


function timeS2timeF ( $t, $format = '%H:%i', $round = false, $limit24=false, $errType= E_USER_NOTICE) {

    // Repair the Input
    
    $round = trim( $round );
    $roundf = 1;
    if ( $round ) {
		$round = strtolower( $round[0] );
		switch ( $round ) {
			case 'm':
				$roundf = 60;
				break;
			case 'h':
				$roundf = 3600;
				break;
		}
    }
    
    // Repair the Input for time:
	
	if ( '' === $t ) {
		$t = 0;
	}
		
	if 			( is_float ( $t ) ) {
		// time ist given in houres (a float)
		$time = $t * 3600;
		$time = $roundf * round ( $time / $roundf );
		if ( ( ! $limit24 ) or $time < 86400 ) {
			return gmdate( $format, $time );
		} else {
			trigger_error( 'function timeS2timeF: input "' . $t . '" is greater then > 24h', $errType );
			return false;
		}
  	} else {
	    // time ist given in houres (a string)
		$time = strtolower( trim( str_replace( [',' ,' ', "\t"], ['.', '', '' ], $t ) ) );  
		
		// string. examples: '12', '12.5', '12,5', '12h', '12.5h':
		if 	( 1 === preg_match( '/^[0-9]*\.{0,1}[0-9]{1,2}h{0,1}$/', $time ) ) {
			$time = floatval ( $time ) * 3600;
			$time = $roundf * round( $time / $roundf );
			if ( ( ! $limit24 ) or $time < 86400 ) {
				return gmdate( $format, $time );
			} else {
				trigger_error( 'function timeS2timeF: input "' . $t . '" is greater then > 24h', $errType );
				return false;				
			}
		}
		
		// string. examples: '12m', '12.5m', '12,5m':
		elseif 	( 1 === preg_match( '/^[0-9]*\.{0,1}[0-9]{1,2}m$/', $time ) ) {
			$time = floatval ( $time ) * 60;
			$time = $roundf * round( $time / $roundf );
			if ( ( ! $limit24 ) or $time < 86400 ) {
				return gmdate( $format, $time );
			} else {
				trigger_error( 'function timeS2timeF: input "' . $t . '" is greater then > 24h', $errType );
				return false;								
			}
		}		

		// string. examples: '12s', '12.5s', '12,5s':
		elseif 	( 1 === preg_match( '/^[0-9]*\.{0,1}[0-9]*s$/', $time ) ) {
			$time = floatval ( $time );
			$time = $roundf * round( $time / $roundf );
			if ( ( ! $limit24 ) or $time < 86400 ) {
				return gmdate( $format, $time );
			} else {
				trigger_error( 'function timeS2timeF: input "' . $t . '" is greater then > 24h', $errType );
				return false;								
			}
		}		

		// string. examples: '2:12:30':
		elseif 	( 1 === preg_match ( '/^[0-9]+:[0-9]{1,2}:[0-9]{1,2}$/', $time ) ) {
			list ($h, $m, $s ) = explode( ':', $time );
			$time =  $h * 3600 + $m * 60 + $s;
			$time = $roundf * round( $time / $roundf );
			if ( ( ! $limit24 ) or $time < 86400 ) {
				return gmdate( $format, $time );
			} else {
				trigger_error( 'function timeS2timeF: input "' . $t . '" is greater then > 24h', $errType );
				return false;								
			}
		}
				
		// string. examples: '12:30':
		elseif 	( 1 === preg_match ( '/^[0-9]+:[0-9]{1,2}$/', $time ) ) {
			list ($h, $m ) = explode( ':', $time );
			$time =  $h * 3600 + $m * 60;
			$time = $roundf * round( $time / $roundf );
			if ( ( ! $limit24 ) or $time < 86400 ) {
				return gmdate( $format, $time );
			} else {
				trigger_error( 'function timeS2timeF: input "' . $t . '" is greater then > 24h', $errType );
				return false;								
			}
		}
		else {
			rigger_error( 'function timeS2timeF: input "' . $t . '" is not detectable', $errType );
			return false;
		}
	}
}