php RSS 提要发布日期的正确格式是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11993258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the correct format for RSS feed pubdate?
提问by Dan Fein
I'm having trouble getting the date of my RSS Feed to run correctly. Do you know what the proper date to show it is?
我无法正确获取 RSS Feed 的日期。你知道显示它的正确日期是什么吗?
I have it stored in a field called creation_date in this format: 2012-08-14 10:17:12
我将它以这种格式存储在名为 creation_date 的字段中:2012-08-14 10:17:12
Then i grab it:
然后我抓住它:
$pubDate = $article[creation_date];
Then I convert it:
然后我转换它:
$pubDate= date("Y-m-d", strtotime($pubDate));
Then within my item tag I place it:
然后在我的项目标签中放置它:
<pubdate>'.date("l, F d, Y", strtotime($pubDate)).'</pubdate>
<pubdate>'.date("l, F d, Y", strtotime($pubDate)).'</pubdate>
Is there something that I'm not seeing?
有什么我没有看到的吗?
采纳答案by Dan Fein
Solved:
解决了:
$pubDate = $article[creation_date];
$pubDate= date("D, d M Y H:i:s T", strtotime($pubDate));
then in my echo'd code:
然后在我的回显代码中:
<pubDate>'.$pubDate.'</pubDate>
回答by Nebel54
The PHP date function has already a way to format pubDate (RFC 2822) compliant dates:
PHP date 函数已经有一种方法可以格式化符合 pubDate (RFC 2822) 的日期:
date('r', $timestamp);
回答by splash
See pubDatedefinition in RSS 2.0 Specification:
请参阅RSS 2.0 规范中的pubDate定义:
All date-times in RSS conform to the Date and Time Specification of RFC 822, with the exception that the year may be expressed with two characters or four characters (four preferred).
Here are examples of valid RFC822 date-times:
以下是有效 RFC822 日期时间的示例:
<pubDate>Wed, 02 Oct 2002 08:00:00 EST</pubDate>
<pubDate>Wed, 02 Oct 2002 13:00:00 GMT</pubDate>
<pubDate>Wed, 02 Oct 2002 15:00:00 +0200</pubDate>
See also Problematical RFC 822 date-time value.
另请参阅有问题的 RFC 822 日期时间值。
回答by Edd
Use this format: D, d M Y H:i:s O. See http://php.net/manual/en/class.datetime.php
使用这种格式:D, d M Y H:i:s O. 见http://php.net/manual/en/class.datetime.php
Or use DateTime constants for more easy usage: DateTime::RSS
或者使用 DateTime 常量更容易使用: DateTime::RSS
回答by CONvid19
Rss pubDateuses the RFC 2822 standards. You can achieve this in phpby invoking the rargument on the datefunction, i.e:
RsspubDate使用RFC 2822 标准。您可以php通过调用日期函数r上的参数来实现这一点,即:
<?php
$pubDate= date('r', time());
echo "<pubDate>$pubDate</pubDate>";
# <pubDate>Thu, 20 Nov 2014 18:59:18 UTC</pubDate>
?>
If you prefer the DateTimeclass, use:
如果您更喜欢DateTime类,请使用:
$pubDate = new DateTime();
echo $pubDate->format(DateTime::RSS);
回答by Charalampos Tsimpouris
While the accepted answer ("D, d M Y H:i:s T")works as expected most of the time, it is not 100% correct. In multilingual situations this string may give non English text which won't be accepted as RFC compliant. To be always sure that the English version is used, use "r".
虽然接受的答案("D, d M Y H:i:s T")在大多数情况下都按预期工作,但并非 100% 正确。在多语言情况下,此字符串可能会提供不符合 RFC 标准的非英文文本。要始终确保使用英文版本,请使用"r".
回答by Paul T. Rawkeen
What about DateTimeobject (PHP 5 >= 5.2.0)
关于什么DateTime对象(PHP 5> = 5.2.0)
\DateTime::createFromFormat(\DateTime::RSS, $RSSDate); // converting RSS date to object
or
或者
date(\DateTime::RSS, $timestamp); // formatting timestamp to RSS time
or both
或两者
$dto = \DateTime::createFromFormat(\DateTime::RSS, $RSSDate);
date('d-M-Y H:i:s', $dto->getTimestamp()); // formatting RSS date to anything you want
or even better
甚至更好
$dto = \DateTime::createFromFormat(\DateTime::RSS, $RSSDate);
$formattedDate = $dto->format('d-M-Y H:i:s');
回答by weszy
The easiest method is to use the DATE_RSS predefined constant (available since PHP 5.1.0).
最简单的方法是使用 DATE_RSS 预定义常量(自 PHP 5.1.0 起可用)。
$pubDate = date(DATE_RSS, strtotime($pubDate));
回答by matao
I have used like this:
我用过这样的:
$item->date = date('D, d M Y H:i:s GMT', strtotime($myBlogPublishedTime));

