2012-02-24 15:07:04 +01:00
|
|
|
<?php
|
|
|
|
|
2012-08-29 16:58:03 +02:00
|
|
|
class LengthFormatter
|
|
|
|
{
|
2012-02-24 15:07:04 +01:00
|
|
|
/**
|
|
|
|
* @string length
|
|
|
|
*/
|
|
|
|
private $_length;
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
// @param string $length formatted H:i:s.u (can be > 24 hours)
|
2012-02-24 15:07:04 +01:00
|
|
|
public function __construct($length)
|
|
|
|
{
|
|
|
|
$this->_length = $length;
|
|
|
|
}
|
|
|
|
|
2012-08-29 16:58:03 +02:00
|
|
|
public function format()
|
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
if (!preg_match('/^[0-9]{2}:[0-9]{2}:[0-9]{2}/', $this->_length)) {
|
2012-08-02 17:17:10 +02:00
|
|
|
return $this->_length;
|
2012-07-31 23:46:37 +02:00
|
|
|
}
|
2012-02-24 15:07:04 +01:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
$pieces = explode(':', $this->_length);
|
2012-02-24 15:07:04 +01:00
|
|
|
|
|
|
|
$seconds = round($pieces[2], 1);
|
|
|
|
$seconds = number_format($seconds, 1);
|
2021-10-11 16:10:47 +02:00
|
|
|
list($seconds, $milliStr) = explode('.', $seconds);
|
2012-02-24 15:07:04 +01:00
|
|
|
|
|
|
|
if (intval($pieces[0]) !== 0) {
|
2021-10-11 16:10:47 +02:00
|
|
|
$hours = ltrim($pieces[0], '0');
|
2012-02-24 15:07:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$minutes = $pieces[1];
|
|
|
|
//length is less than 1 hour
|
|
|
|
if (!isset($hours)) {
|
|
|
|
if (intval($minutes) !== 0) {
|
2021-10-11 16:10:47 +02:00
|
|
|
$minutes = ltrim($minutes, '0');
|
2012-02-24 15:07:04 +01:00
|
|
|
}
|
|
|
|
//length is less than 1 minute
|
|
|
|
else {
|
|
|
|
unset($minutes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
if (isset($hours, $minutes, $seconds)) {
|
|
|
|
$time = sprintf('%d:%02d:%02d.%s', $hours, $minutes, $seconds, $milliStr);
|
|
|
|
} elseif (isset($minutes, $seconds)) {
|
|
|
|
$time = sprintf('%d:%02d.%s', $minutes, $seconds, $milliStr);
|
2012-08-29 16:58:03 +02:00
|
|
|
} else {
|
2021-10-11 16:10:47 +02:00
|
|
|
$time = sprintf('%d.%s', $seconds, $milliStr);
|
2012-02-24 15:07:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $time;
|
|
|
|
}
|
2012-08-29 16:58:03 +02:00
|
|
|
}
|