如何正确格式化 xml 站点地图的上次修改 (lastmod) 时间

How to properly format last modified (lastmod) time for xml sitemaps

我正在创建一个应用程序,每次在网站上添加或更新新内容时,它都会自动更新 sitemap.xml

根据 Google 的最佳实践,<lastmod></lastmod> 标签的格式应如下所示:

<lastmod>2011-06-27T19:34:00+01:00</lastmod>

我的问题涉及时间格式本身。我理解 2011-06-27T19:34:00 部分。我不明白的是 +01:00,我假设是 +/- UTC

这个假设正确吗?

我的时区 Table 看起来像这样:

因此,如果该站点位于 #4 阿富汗,则正确时间为: 2011-06-27T19:34:00+04:00

如果该站点基于#6 阿拉斯加标准时间,则正确时间为:2011-06-27T19:34:00-09:00

我的假设是正确的还是我没有正确理解+01:00

lastmod 标记在站点地图中是可选的,在大多数情况下它会被搜索引擎忽略,因为网站管理员在保持它的准确性方面做得很糟糕。无论如何,您都可以使用它,格式取决于您的能力和要求;如果您不能或不想提供时区偏移量,您实际上不必提供时区偏移量,您也可以选择使用简单的 YYYY-MM-DD。

来自Lastmod definition section of sitemaps.org

The date of last modification of the file. This date should be in W3C Datetime format. This format allows you to omit the time portion, if desired, and use YYYY-MM-DD.

如果您想细化到那个粒度并提供时区偏移量,那么您是对的,它是 UTC +/-。来自 W3C Datetime:

Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC. A time zone offset of "-hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes behind UTC.

和示例,仍然来自 W3C:

1994-11-05T08:15:30-05:00 corresponds to November 5, 1994, 8:15:30 am, US Eastern Standard Time.

在 C# 中正确格式化 XML 站点地图的上次修改 (lastmod) 时间

var ss = DateTime.Now.ToString("yyyy-mm-ddThh:mm:ss:zzz");

在PHP中,您可以使用:

$lastmod = date("Y-m-d\Th:m:s+00:00");

这将显示如下内容:

2018-02-14T08:02:28+00:00

PHP 实际上应该是:

date('Y-m-d\TH:i:s+00:00');

Google 站点地图 XML 中 C#lastmod 字段的格式如下:

var lastmod = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz");

它提供诸如 <lastmod>2018-08-24T09:18:38-04:00</lastmod> 之类的值,这是 W3C Datetime 格式。

Date.prototype.toISOString()

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively). The timezone is always zero UTC offset, as denoted by the suffix "Z".

// expected output: 2011-10-05T14:48:00.000Z