php 谷歌站点地图日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11780404/
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
Google Sitemap Date Format
提问by NXT
I need date format for sitemaps in php.
我需要 php 中站点地图的日期格式。
How can i do that ?
我怎样才能做到这一点 ?
Is this output right ?
这个输出对吗?
<lastmod>2012-08-02EEST18:01:18+03:00</lastmod>
回答by Dunhamzzz
If you have the timestamp you can format the date correctly like this:
如果您有时间戳,您可以像这样正确格式化日期:
<lastmod><?php echo date('c', $timestamp); ?></lastmod>
Time stamp could be time()(for current time) or some other method of fetching a unix tiemstamp like strtotime()
时间戳可以是time()(对于当前时间)或其他一些获取 unix 时间戳的方法,例如strtotime()
回答by Erfan Safarpoor
To format the current timestamp in W3C Datetime encoding (as used in sitemap.xml files) use the following parameters.
要以 W3C 日期时间编码(如在 sitemap.xml 文件中使用)格式化当前时间戳,请使用以下参数。
echo date('Y-m-dTH:i:sP', time());
As of PHP5 you can also use the c format character to print the exact same string.
从 PHP5 开始,您还可以使用 c 格式字符打印完全相同的字符串。
echo date('c',time());
These would both print out the following:
这些都将打印出以下内容:
2009-04-03T11:49:00+01:00
回答by NXT
To convert from a MySQL's datetime format to the one needed for lastmodin sitemaps, just use the PHP code below.
要将 MySQL 的日期时间格式转换为站点地图中lastmod所需的格式,只需使用下面的 PHP 代码。
$lastmod = '2012-11-28 10:53:17'; //MySQL datetime format
$datetime = new DateTime($lastmod);
$result = $datetime->format('Y-m-d\TH:i:sP');
echo $result; //2012-11-28T10:53:17+01:00
回答by xiatica
According to Google, he value is an optional field that, if provided, must be formatted as
根据 Google,他的值是一个可选字段,如果提供,必须格式化为
YYYY-MM-DDThh:mmTZD
The default export of an SQL datetime is dependent on the language setting for your server, and may include spaces and characterized TZD such as:
SQL 日期时间的默认导出取决于服务器的语言设置,并且可能包含空格和特征化的 TZD,例如:
2014-09-19 10:33:05 UTC
The W3 specifythat TZD can be formatted in any of three options
W3 指定可以在三个选项中的任何一个中格式化 TZD
TZD = time zone designator (Z or +hh:mm or -hh:mm)
TZD = 时区指示符(Z 或 +hh:mm 或 -hh:mm)
Any spaces from your default formatting must be stripped; the character 'T' must be inserted before time, and the TZD must match one of the supported types.
必须去除默认格式中的任何空格;字符 'T' 必须在时间之前插入,并且 TZD 必须匹配支持的类型之一。
Therefore to format the current timestamp, use the following parameters.
因此,要格式化当前时间戳,请使用以下参数。
echo date('Y-m-dTH:i:sP', time());
As of PHP5 you can also use the c format character to print the exact same string.
从 PHP5 开始,您还可以使用 c 格式字符打印完全相同的字符串。
echo date('c',time());
回答by Alex K
I know the question was about PHP, but I found this page googling for Node/JavaScript. So the closest equivalent is Date.toISOString():
我知道这个问题是关于 PHP 的,但我发现这个页面在谷歌上搜索 Node/JavaScript。所以最接近的等价物是Date.toISOString():
var d = new Date();
d.toISOString(); // "2017-04-19T07:23:16.040Z"
回答by AdamJones
Your getting this output because you used the old recommended timestamp string of
您获得此输出是因为您使用了旧的推荐时间戳字符串
Y-m-dTH:i:sP.
However, what you need is
然而,你需要的是
Y-m-d\TH:i:sP
This single back slash will make the difference. At some point a capital T became a special character in PHP signifying timecode, so if you don't escape the T you will get exactly what your reporting.
这个单反斜杠会有所作为。在某些时候,大写的 T 成为 PHP 中表示时间码的特殊字符,因此如果您不转义 T,您将获得准确的报告内容。
回答by John Conde
You have a lot of formats to choose from. Using the cparameter with the date()function will get the recommended format for you.
您有多种格式可供选择。将c参数与date()函数一起使用将为您获得推荐的格式。
回答by user3152317
delete "EES" Mine is like this 2013-12-26T19:00:00-05:00 and it works http://www.industry-automation-parts.com/1_e_0_sitemap.xml
删除“EES” 我的就像这样 2013-12-26T19:00:00-05:00 它可以工作 http://www.industry-automation-parts.com/1_e_0_sitemap.xml
回答by Jodyshop
Actually, there are 2 approaches to this.
实际上,有两种方法可以解决这个问题。
The firstapproach is to set a valid date for XML sitemap which includes the "C" char which gave the value: (2019-06-03T17:30:21-05:00). Test it here
第一种方法是为 XML 站点地图设置一个有效日期,其中包含给出值的“C”字符:(2019-06-03T17:30:21-05:00)。在这里测试
<lastmod>'.date('c', strtotime($row['date'])).'</lastmod>
<lastmod>'.date('c', strtotime($row['date'])).'</lastmod>
The 2ndapproach is to set a valid date for RSS sitemap which includes the "r" char which gave the value: (2019-03-07T17:55:44+00:00).
的第二方法是设置为RSS站点地图,其中包括了“R”炭这给了值的有效日期:(2019-03-07T17:55:44 + 00:00) 。
<pubDate>'.date('r', strtotime($row['date'])).'</pubDate>
<pubDate>'.date('r', strtotime($row['date'])).'</pubDate>
Hope this helps you. Thanks
希望这对你有帮助。谢谢
回答by Ahmet ?im?ek
with moment.js;
使用moment.js;
moment().format('YYY-MM-DDTHH:mm:ssZ')

