Grav\Plugin\Admin\AdminTwigExtension::adminNicetimeFilter PHP Method

adminNicetimeFilter() public method

public adminNicetimeFilter ( $date, $long_strings = true )
    public function adminNicetimeFilter($date, $long_strings = true)
    {
        if (empty($date)) {
            return $this->grav['admin']->translate('NICETIME.NO_DATE_PROVIDED', null, true);
        }
        if ($long_strings) {
            $periods = ["NICETIME.SECOND", "NICETIME.MINUTE", "NICETIME.HOUR", "NICETIME.DAY", "NICETIME.WEEK", "NICETIME.MONTH", "NICETIME.YEAR", "NICETIME.DECADE"];
        } else {
            $periods = ["NICETIME.SEC", "NICETIME.MIN", "NICETIME.HR", "NICETIME.DAY", "NICETIME.WK", "NICETIME.MO", "NICETIME.YR", "NICETIME.DEC"];
        }
        $lengths = ["60", "60", "24", "7", "4.35", "12", "10"];
        $now = time();
        // check if unix timestamp
        if ((string) (int) $date == $date) {
            $unix_date = $date;
        } else {
            $unix_date = strtotime($date);
        }
        // check validity of date
        if (empty($unix_date)) {
            return $this->grav['admin']->translate('NICETIME.BAD_DATE', null, true);
        }
        // is it future date or past date
        if ($now > $unix_date) {
            $difference = $now - $unix_date;
            $tense = $this->grav['admin']->translate('NICETIME.AGO', null, true);
        } else {
            $difference = $unix_date - $now;
            $tense = $this->grav['admin']->translate('NICETIME.FROM_NOW', null, true);
        }
        for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) {
            $difference /= $lengths[$j];
        }
        $difference = round($difference);
        if ($difference != 1) {
            $periods[$j] .= '_PLURAL';
        }
        if ($this->grav['language']->getTranslation($this->grav['user']->language, $periods[$j] . '_MORE_THAN_TWO')) {
            if ($difference > 2) {
                $periods[$j] .= '_MORE_THAN_TWO';
            }
        }
        $periods[$j] = $this->grav['admin']->translate($periods[$j], null, true);
        return "{$difference} {$periods[$j]} {$tense}";
    }

Usage Example

 protected function taskGetNewsFeed()
 {
     $cache = $this->grav['cache'];
     if ($this->post['refresh'] == 'true') {
         $cache->delete('news-feed');
     }
     $feed_data = $cache->fetch('news-feed');
     if (!$feed_data) {
         try {
             $feed = $this->admin->getFeed();
             if (is_object($feed)) {
                 require_once __DIR__ . '/../twig/AdminTwigExtension.php';
                 $adminTwigExtension = new AdminTwigExtension();
                 $feed_items = $feed->getItems();
                 // Feed should only every contain 10, but just in case!
                 if (count($feed_items) > 10) {
                     $feed_items = array_slice($feed_items, 0, 10);
                 }
                 foreach ($feed_items as $item) {
                     $datetime = $adminTwigExtension->adminNicetimeFilter($item->getDate()->getTimestamp());
                     $feed_data[] = '<li><span class="date">' . $datetime . '</span> <a href="' . $item->getUrl() . '" target="_blank" title="' . str_replace('"', '″', $item->getTitle()) . '">' . $item->getTitle() . '</a></li>';
                 }
             }
             // cache for 1 hour
             $cache->save('news-feed', $feed_data, 60 * 60);
         } catch (\Exception $e) {
             $this->admin->json_response = ['status' => 'error', 'message' => $e->getMessage()];
             return;
         }
     }
     $this->admin->json_response = ['status' => 'success', 'feed_data' => $feed_data];
 }
All Usage Examples Of Grav\Plugin\Admin\AdminTwigExtension::adminNicetimeFilter