Google Sitemap Date Format
Asked Answered
P

10

28

I need date format for sitemaps in php.

How can i do that ?

Is this output right ?

<lastmod>2012-08-02EEST18:01:18+03:00</lastmod>
Petit answered 2/8, 2012 at 15:4 Comment(1)
do you mean sitemaps grabbed by bots ?Swartz
S
46

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()

Solangesolano answered 2/8, 2012 at 15:7 Comment(2)
i'm recieving lastmod data from database. and it's datetime format. I'm using strtotime to convert. if type is correct for google it works. Thank you.Petit
and if you have date string like 2023-12-14 20:38 you can use date('c', strtotime($dateTime));Gymnastics
S
28

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

Swagerty answered 11/10, 2013 at 16:30 Comment(3)
The date string you give is the case of @Petit 's problem. I have posted the correct string as an answer.Fantasm
@Fantasm and the answer from @NXT are correct: if you're using the date format string Y-m-dTH:i:sP, you need to escape the T i.e. Y-m-d\TH:i:sP. However using the PHP5 option date('c',time()) works perfectly.Aeriform
@Aeriform Works for me i have the problem with the "T" char the problem fix with the escape ! Thanks !Annates
W
23

To convert from a MySQL's datetime format to the one needed for lastmod in sitemaps, just use the PHP code below.

$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
Wicopy answered 28/11, 2012 at 10:28 Comment(1)
Could use shorter format $result = $datetime->format('c');. `Auto
M
12

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());
Melantha answered 28/9, 2014 at 4:46 Comment(0)
C
6

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"
Coumas answered 19/4, 2017 at 7:28 Comment(1)
If there isn't an question & answer already on SO for a javascript version, you should consider adding this as a new question for other users who are also searching for this too! See Answering your own questionAeriform
F
4

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.

Fantasm answered 28/8, 2014 at 23:35 Comment(0)
C
2

You have a lot of formats to choose from. Using the c parameter with the date() function will get the recommended format for you.

Cyprio answered 2/8, 2012 at 15:7 Comment(0)
T
2

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

Term answered 5/6, 2019 at 0:44 Comment(0)
D
1

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

Depository answered 2/1, 2014 at 2:48 Comment(0)
S
0

with moment.js;

moment().format('YYY-MM-DDTHH:mm:ssZ')
Secundines answered 13/2, 2019 at 8:51 Comment(1)
Can you explain that further? I don't think that moment.js is available for PHP after allChor

© 2022 - 2024 — McMap. All rights reserved.