FeedWriter\Item::setDate PHP Method

setDate() public method

The value of the date parameter can be either an instance of the DateTime class, an integer containing a UNIX timestamp or a string which is parseable by PHP's 'strtotime' function.
public setDate ( DateTim\DateTime | integer | string $date ) : self
$date DateTim\DateTime | integer | string Date which should be used.
return self
    public function setDate($date)
    {
        if (!is_numeric($date)) {
            if ($date instanceof DateTime) {
                $date = $date->getTimestamp();
            } else {
                $date = strtotime($date);
                if ($date === FALSE) {
                    throw new \InvalidArgumentException('The given date string was not parseable.');
                }
            }
        } elseif ($date < 0) {
            throw new \InvalidArgumentException('The given date is not an UNIX timestamp.');
        }
        if ($this->version == Feed::ATOM) {
            $tag = 'updated';
            $value = date(\DATE_ATOM, $date);
        } elseif ($this->version == Feed::RSS2) {
            $tag = 'pubDate';
            $value = date(\DATE_RSS, $date);
        } else {
            $tag = 'dc:date';
            $value = date("Y-m-d", $date);
        }
        return $this->addElement($tag, $value);
    }

Usage Example

コード例 #1
0
ファイル: PostsController.php プロジェクト: aisuhua/blog-1
 /**
  * Handles the RSS action. Constructs the rss feed of the latest posts. The
  * number of posts to return is stored in the configuration section
  *
  * @return Response
  */
 public function rssAction()
 {
     $feed = new RSS2();
     $feed->setEncoding('UTF-8');
     $feed->setTitle($this->config->rss->title);
     $feed->setDescription($this->config->rss->description);
     $feed->setLink($this->getFullUrl());
     $posts = $this->finder->getLatest(1);
     foreach ($posts as $post) {
         $feedItem = new Item();
         $feedItem->setTitle($post->getTitle());
         $feedItem->setLink($this->getFullUrl('/post/' . $post->getSlug()));
         $feedItem->setDescription($post->getContent());
         $feedItem->setDate($post->getDate());
         $feed->addItem($feedItem);
     }
     $response = new Response();
     $response->setHeader('Content-Type', 'application/xml');
     $response->setContent($feed->generateFeed());
     return $response;
 }