I need date format for sitemaps in php.
How can i do that ?
Is this output right ?
2012-08-02EEST18:01:18+03:00
To format the current timestamp in W3C Datetime encoding (as used in sitemap.xml files) 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.
echo date('c',time());
These would both print out the following:
2009-04-03T11:49:00+01:00
SOURCE
I know the question was about PHP, but I found this page googling for Node/JavaScript. So the closest equivalent is Date.toISOString()
:
var d = new Date();
d.toISOString(); // "2017-04-19T07:23:16.040Z"
According to Google, he value is an optional field that, if provided, must be formatted as
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:
2014-09-19 10:33:05 UTC
The W3 specify that TZD can be formatted in any of three options
TZD = time zone designator (Z or +hh:mm or -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.
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.
echo date('c',time());
Actually, there are 2 approaches to this.
The first approach 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
<lastmod>'.date('c', strtotime($row['date'])).'</lastmod>
The 2nd approach 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).
<pubDate>'.date('r', strtotime($row['date'])).'</pubDate>
Hope this helps you. Thanks