怎么能把(美国/芝加哥)时间改成印度(亚洲/加尔各答)时间



$row['created'] <-一个变量获取日期/时间到数据库。$row['created'] have time like 2016-10-07 01:31:42 .,时区(America/Chicago)。所以我希望按照印度时区显示时间。我正在使用如下所示的代码,但不工作。

$timestamp = strtotime($row['created']);
    date_default_timezone_set("Asia/Kolkata");
    echo date('r', $timestamp);

试试这段代码来节省印度时间。

<?php
    $date=date_create('2016-10-07 01:26:56',timezone_open("America/Chicago"));
    echo date_format($date,"Y-m-d H:i:sP")."<br>";
    date_timezone_set($date,timezone_open("Asia/Kolkata"));
    $indiatime = date_format($date,"Y-m-d H:i:sP") . "<br>";
    echo $indiatime ;
?>

可以使用DateTime类。看一下这个DateTime文档的例子。

$date = new DateTime($row['created'], new DateTimeZone('America/Chicago'));
echo $date->format('Y-m-d H:i:sP') . "n";
$date->setTimezone(new DateTimeZone('Asia/Kolkata'));
echo $date->format('Y-m-d H:i:sP') . "n";

试试这个,使用timezone_open和date_format

$timestamp = "2016-10-07 01:31:42";
$date = date_create($timestamp, timezone_open('Asia/Kolkata'));
echo date_format($date, 'Y-m-d H:i:sP') . "n";  //echo 2016-10-07 01:31:42+05:30

$timestamp = "2016-10-07 01:31:42";
$user_tz = 'Asia/Kolkata';    
echo $timestamp. "n"; // echo 2016-10-07 01:31:42
$schedule_date = new DateTime($timestamp, new DateTimeZone($user_tz) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$triggerOn =  $schedule_date->format('Y-m-d H:i:s');
echo $triggerOn; // echo 2016-10-06 20:01:42

如果你正在使用mysql,你也这样做:

SELECT CONVERT_TZ(created,'America/Chicago','Asia/Kolkata');

而不是在php编码,当你从数据库中获取数据,你可以使用以下查询:select field1,field2,created from <tablename> where <yourCondition>。而不是这样更新你的查询:

select field1,field2,CONVERT_TZ(created,'America/Chicago','Asia/Kolkata') from <tablename> where <yourCondition>

相关内容

  • 没有找到相关文章