<?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
$roundf = 1;
if ( $round ) {
switch ( $round ) {
case 'm':
$roundf = 60;
break;
case 'h':
$roundf = 3600;
break;
}
}
// Repair the Input for time:
if ( '' === $t ) {
$t = 0;
}
// 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)
// 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 = $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 = $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 = $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 ) ) {
$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 ) ) {
$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;
}
}
}