简体   繁体   English

在php中以YYYY-MM-DDThh:mmTZD格式输出日期

[英]Output date in YYYY-MM-DDThh:mmTZD format in php

I'm building a sitemap to submit to Google for better crawling through my website. 我正在构建一个站点地图,以便提交给Google,以便更好地在我的网站中进行爬网。

I have a date column in MYSQL database and I want the xml tag <lastmod> to contain date fetched from MYSQL database in YYYY-MM-DDThh:mmTZD format. 我在MYSQL数据库中有一个日期列,我希望xml标签<lastmod>包含从MYSQL数据库中以YYYY-MM-DDThh:mmTZD格式获取的日期。

Example

If MYSQL query returns 2015-07-29 , I want this to be printed in YYYY-MM-DDThh:mmTZD format . 如果MYSQL查询返回2015-07-29 ,我希望将其以YYYY-MM-DDThh:mmTZD format打印。 However, I don't have a time column in database and I want it to be set to 12 am according to timezone of India. 但是,我在数据库中没有时间栏,我希望根据印度的时区将其设置为上午12点。 I've tried to do so but get incompatible date format error. 我曾尝试这样做,但出现incompatible date format错误。

Working with timezones in PHP is pretty easy. 在PHP中使用时区非常容易。 Below follows an example: 下面是一个示例:

// Create a new DateTime object
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));

// Output
echo $date->format('Y-m-d H:i:sP') . "\n";

// Change timezone
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));

// Output again
echo $date->format('Y-m-d H:i:sP') . "\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM