Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

sitemap

I am creating an app that will automatically update sitemap.xml each time new content is added to, or updated on the site.

According to Google's best practices the <lastmod></lastmod> tag should be formatted as follows:

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

My question concerns the time formatting itself. I understand the 2011-06-27T19:34:00 part. What I do not understand is the +01:00, which I am assuming is the +/- UTC.

Is this a correct assumption?

My Time Zone Table looks like this:

enter image description here

So if the site was based in #4 Afghanistan the correct time would be: 2011-06-27T19:34:00+04:00

And if the site was based in #6 Alaska Standard Time the correct time would be: 2011-06-27T19:34:00-09:00

Is my assumption correct or am I not correctly understanding the +01:00?

like image 481
petebolduc Avatar asked Jul 10 '15 20:07

petebolduc


People also ask

What is Lastmod in Sitemap XML?

lastmod : the date of when the content on that URL was last modified.

What is XML sitemap What is the use of sitemap give layout of XML sitemap?

An XML sitemap is a file that lists a website's important pages, making sure Google can find and crawl them all. It also helps search engines understand your website structure. You want Google to crawl every essential page of your website.

What should XML Sitemap contain?

What Is an XML Sitemap. In simple terms, an XML sitemap is a list of your website's URLs. It acts as a roadmap to tell search engines what content is available and how to reach it. In the example above, a search engine will find all nine pages in a sitemap with one visit to the XML sitemap file.


6 Answers

The lastmod tag is optional in sitemaps and in most of the cases it's ignored by search engines, because webmasters are doing a horrible job keeping it accurate. In any case, you may use it, and the format depends on your capabilities and requirements; you don't actually have to provide a timezone offset if you can't or don't want to, you can choose to go with a simple YYYY-MM-DD as well.

From the 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.

If you want to go down to that granularity and provide the timezone offset as well, you're correct, it's UTC +/-. From 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.

And example, still from W3C:

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

like image 117
methode Avatar answered Sep 20 '22 17:09

methode


Properly format last modified (lastmod) time for XML sitemaps in C#

var ss = DateTime.Now.ToString("yyyy-mm-ddThh:mm:ss:zzz"); 
like image 27
Safavi Avatar answered Sep 20 '22 17:09

Safavi


The format for the lastmod field in Google Sitemaps XML for C# is the following:

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

It provides values such as <lastmod>2018-08-24T09:18:38-04:00</lastmod> which is the W3C Datetime format.

like image 41
truemedia Avatar answered Sep 18 '22 17:09

truemedia


In PHP, you can use :

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

This will display something like:

2018-02-14T08:02:28+00:00
like image 36
Fifi Avatar answered Sep 20 '22 17:09

Fifi


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

like image 20
Ronnie Royston Avatar answered Sep 19 '22 17:09

Ronnie Royston


PHP should actually be:

date('Y-m-d\TH:i:s+00:00');
like image 22
Mitch Avatar answered Sep 18 '22 17:09

Mitch