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>
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>
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()
2023-12-14 20:38
you can use date('c', strtotime($dateTime));
–
Gymnastics 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
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 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
$result = $datetime->format('c');
. ` –
Auto 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());
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"
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.
You have a lot of formats to choose from. Using the c
parameter with the date()
function will get the recommended format for you.
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
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
with moment.js;
moment().format('YYY-MM-DDTHH:mm:ssZ')
moment.js
is available for PHP after all –
Chor © 2022 - 2024 — McMap. All rights reserved.