Tools\Utility\Time::fuzzyFromOffset PHP Method

fuzzyFromOffset() public static method

public static fuzzyFromOffset ( integer $offset, boolean | null $past = null ) : string
$offset integer in seconds
$past boolean | null (defaults to null: return plain text) - new: if not boolean but a string use this as translating text
return string text (i18n!)
    public static function fuzzyFromOffset($offset, $past = null)
    {
        if ($offset <= MINUTE) {
            $span = 'moments';
        } elseif ($offset < MINUTE * 20) {
            $span = 'a few minutes';
        } elseif ($offset < HOUR) {
            $span = 'less than an hour';
        } elseif ($offset < HOUR * 4) {
            $span = 'a couple of hours';
        } elseif ($offset < DAY) {
            $span = 'less than a day';
        } elseif ($offset < DAY * 2) {
            $span = 'about a day';
        } elseif ($offset < DAY * 4) {
            $span = 'a couple of days';
        } elseif ($offset < WEEK) {
            $span = 'less than a week';
        } elseif ($offset < WEEK * 2) {
            $span = 'about a week';
        } elseif ($offset < MONTH) {
            $span = 'less than a month';
        } elseif ($offset < MONTH * 2) {
            $span = 'about a month';
        } elseif ($offset < MONTH * 4) {
            $span = 'a couple of months';
        } elseif ($offset < YEAR) {
            $span = 'less than a year';
        } elseif ($offset < YEAR * 2) {
            $span = 'about a year';
        } elseif ($offset < YEAR * 4) {
            $span = 'a couple of years';
        } elseif ($offset < YEAR * 8) {
            $span = 'a few years';
        } elseif ($offset < YEAR * 12) {
            $span = 'about a decade';
        } elseif ($offset < YEAR * 24) {
            $span = 'a couple of decades';
        } elseif ($offset < YEAR * 64) {
            $span = 'several decades';
        } else {
            $span = 'a long time';
        }
        if ($past === true) {
            // This is in the past
            return __d('tools', '%s ago', __d('tools', $span));
        }
        if ($past === false) {
            // This in the future
            return __d('tools', 'in %s', __d('tools', $span));
        }
        if ($past !== null) {
            // Custom translation
            return __d('tools', $past, __d('tools', $span));
        }
        return __d('tools', $span);
    }

Usage Example

コード例 #1
0
ファイル: TimeTest.php プロジェクト: alescx/cakephp-tools
 /**
  * TimeTest::testFuzzyFromOffset()
  *
  * @return void
  */
 public function testFuzzyFromOffset()
 {
     //$this->out($this->_header(__FUNCTION__), true);
     $ret = $this->Time->fuzzyFromOffset(MONTH);
     //pr($ret);
     $ret = $this->Time->fuzzyFromOffset(120);
     //pr($ret);
     $ret = $this->Time->fuzzyFromOffset(DAY);
     //pr($ret);
     $ret = $this->Time->fuzzyFromOffset(DAY + 2 * MINUTE);
     //pr($ret);
     // FIX ME! Doesn't work!
     $ret = $this->Time->fuzzyFromOffset(-DAY);
     //pr($ret);
 }