Pubwich::time_since PHP Method

time_since() public static method

Return a date in a relative format Based on: http://snippets.dzone.com/posts/show/5565
public static time_since ( $original ) : string
$original Date timestamp
return string
    public static function time_since($original)
    {
        $original = strtotime($original);
        $chunks = array(array(60 * 60 * 24 * 365, Pubwich::_('year')), array(60 * 60 * 24 * 30, Pubwich::_('month')), array(60 * 60 * 24 * 7, Pubwich::_('week')), array(60 * 60 * 24, Pubwich::_('day')), array(60 * 60, Pubwich::_('hour')), array(60, Pubwich::_('minute')));
        $today = time();
        $since = $today - $original;
        if ($since < 0) {
            return sprintf(Pubwich::_('just moments ago'), $since);
        }
        if ($since < 60) {
            return sprintf(Pubwich::_('%d seconds ago'), $since);
        }
        if ($since > 7 * 24 * 60 * 60) {
            $print = strftime(Pubwich::_('%e %B at %H:%M'), $original);
            return $print;
        }
        for ($i = 0, $j = count($chunks); $i < $j; $i++) {
            $seconds = $chunks[$i][0];
            $name = $chunks[$i][1];
            if (($count = floor($since / $seconds)) != 0) {
                break;
            }
        }
        $suffixe = "s";
        $print = $count == 1 ? '1&nbsp;' . $name : $count . '&nbsp;' . $name . $suffixe;
        return sprintf(Pubwich::_('%s ago'), $print);
    }

Usage Example

Example #1
0
 /**
  * @return array
  */
 public function populateItemTemplate(&$item)
 {
     $comments_count = $item->children('http://purl.org/rss/1.0/modules/slash/')->comments;
     $content = $item->children('http://purl.org/rss/1.0/modules/content/')->encoded;
     $tags = '';
     // pull tags out and make links of them
     foreach ($item->category as $t) {
         $tags .= '<li class="tag"><a href="' . $t->attributes() . htmlspecialchars($t) . '">' . htmlspecialchars($t) . '</a></li>';
     }
     if ($tags != '') {
         $tags = '<ul class="tags">' . $tags . '<li>Tagged:</li></ul>';
     }
     return array('link' => htmlspecialchars($item->link), 'title' => trim($item->title), 'date' => Pubwich::time_since($item->pubDate), 'comments_link' => $item->comments, 'comments_count' => $comments_count, 'description' => $item->description, 'content' => $content, 'author' => $item->author, 'tags' => $tags);
 }
All Usage Examples Of Pubwich::time_since