27

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>

10 Answers10

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

Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
  • 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. –  Aug 02 '12 at 15:10
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

Erfan Safarpoor
  • 5,109
  • 4
  • 23
  • 27
  • 2
    The date string you give is the case of @user1329212 's problem. I have posted the correct string as an answer. – AdamJones Aug 28 '14 at 23:36
  • 3
    @AdamJones 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. – FluffyKitten Mar 29 '18 at 01:14
  • 1
    @FluffyKitten Works for me i have the problem with the "T" char the problem fix with the escape ! Thanks ! – Jasson Rojas May 31 '21 at 04:46
21

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
NXT
  • 1,981
  • 1
  • 24
  • 30
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());
Community
  • 1
  • 1
xiatica
  • 1,546
  • 2
  • 20
  • 24
5

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"
Alex K
  • 6,737
  • 9
  • 41
  • 63
  • 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 question](https://stackoverflow.com/help/self-answer) – FluffyKitten Mar 29 '18 at 01:11
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.

AdamJones
  • 652
  • 1
  • 15
  • 38
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.

John Conde
  • 217,595
  • 99
  • 455
  • 496
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

Jodyshop
  • 656
  • 8
  • 12
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

0

with moment.js;

moment().format('YYY-MM-DDTHH:mm:ssZ')
Ahmet Şimşek
  • 1,391
  • 1
  • 14
  • 24