Tools\Utility\Time::lengthOfTime PHP Method

lengthOfTime() public static method

Time length to human readable format.
See also: timeAgoInWords()
public static lengthOfTime ( integer $seconds, string | null $format = null, array $options = [] ) : string
$seconds integer
$format string | null
$options array - boolean v: verbose - boolean zero: if false: 0 days 5 hours => 5 hours etc. - int: accuracy (how many sub-formats displayed?) //TODO
return string
    public static function lengthOfTime($seconds, $format = null, array $options = [])
    {
        $defaults = ['verbose' => true, 'zero' => false, 'separator' => ', ', 'default' => ''];
        $options += $defaults;
        if (!$options['verbose']) {
            $s = ['m' => 'mth', 'd' => 'd', 'h' => 'h', 'i' => 'm', 's' => 's'];
            $p = $s;
        } else {
            $s = ['m' => ' ' . __d('tools', 'Month'), 'd' => ' ' . __d('tools', 'Day'), 'h' => ' ' . __d('tools', 'Hour'), 'i' => ' ' . __d('tools', 'Minute'), 's' => ' ' . __d('tools', 'Second')];
            $p = ['m' => ' ' . __d('tools', 'Months'), 'd' => ' ' . __d('tools', 'Days'), 'h' => ' ' . __d('tools', 'Hours'), 'i' => ' ' . __d('tools', 'Minutes'), 's' => ' ' . __d('tools', 'Seconds')];
        }
        if (!isset($format)) {
            if (floor($seconds / DAY) > 0) {
                $format = 'Dh';
            } elseif (floor($seconds / 3600) > 0) {
                $format = 'Hi';
            } elseif (floor($seconds / 60) > 0) {
                $format = 'Is';
            } else {
                $format = 'S';
            }
        }
        $ret = '';
        $j = 0;
        $length = mb_strlen($format);
        for ($i = 0; $i < $length; $i++) {
            switch (mb_substr($format, $i, 1)) {
                case 'D':
                    $str = floor($seconds / 86400);
                    break;
                case 'd':
                    $str = floor($seconds / 86400 % 30);
                    break;
                case 'H':
                    $str = floor($seconds / 3600);
                    break;
                case 'h':
                    $str = floor($seconds / 3600 % 24);
                    break;
                case 'I':
                    $str = floor($seconds / 60);
                    break;
                case 'i':
                    $str = floor($seconds / 60 % 60);
                    break;
                case 'S':
                    $str = $seconds;
                    break;
                case 's':
                    $str = floor($seconds % 60);
                    break;
                default:
                    return '';
            }
            if ($str > 0 || $j > 0 || $options['zero'] || $i === mb_strlen($format) - 1) {
                if ($j > 0) {
                    $ret .= $options['separator'];
                }
                $j++;
                $x = mb_strtolower(mb_substr($format, $i, 1));
                if ($str == 1) {
                    $ret .= $str . $s[$x];
                } else {
                    $title = $p[$x];
                    if (!empty($options['plural'])) {
                        if (mb_substr($title, -1, 1) === 'e') {
                            $title .= $options['plural'];
                        }
                    }
                    $ret .= $str . $title;
                }
            }
        }
        return $ret;
    }

Usage Example

コード例 #1
0
ファイル: TimeTest.php プロジェクト: alescx/cakephp-tools
 /**
  * TimeTest::testLengthOfTime()
  *
  * @return void
  */
 public function testLengthOfTime()
 {
     //$this->out($this->_header(__FUNCTION__), true);
     $ret = $this->Time->lengthOfTime(60);
     //pr($ret);
     // FIX ME! Doesn't work!
     $ret = $this->Time->lengthOfTime(-60);
     //pr($ret);
     $ret = $this->Time->lengthOfTime(-121);
     //pr($ret);
     $this->assertEquals('6 ' . __d('tools', 'Minutes') . ', 40 ' . __d('tools', 'Seconds'), $this->Time->lengthOfTime(400));
     $res = $this->Time->lengthOfTime(400, 'i');
     //pr($res);
     $this->assertEquals('6 ' . __d('tools', 'Minutes'), $res);
     $res = $this->Time->lengthOfTime(6 * DAY);
     //pr($res);
     $this->assertEquals('6 ' . __d('tools', 'Days') . ', 0 ' . __d('tools', 'Hours'), $res);
 }