Pranit
Pranit

Reputation: 353

Yii2 could not convert local time to GMT timezone

I am new to yii2, my question is when I trying to convert local to GMT it can not be converted into GMT, I don't know the issue.

$time = gmdate('H:i', strtotime('17:20'));

its returns 17:20. expected output -: 11:50 (converted into GMT). Is any issue in yii2 or any config setting. because above code works good in core php.

Upvotes: 1

Views: 483

Answers (2)

Pranit
Pranit

Reputation: 353

I found the answer, the default timezone of yii2 is in UTC because of this I cant convert UTC to UTC. For this I get local timezone using jquery (without affecting yii2 and server default settings), and convert the time using local timezone to UTC.

$date = new \DateTime($postData['ArrivalTime'], new \DateTimeZone('Asia/Kolkata'));
 $date->format('H:i');
 $date->setTimezone(new \DateTimeZone('UTC'));
 $Time = $date->format('H:i'); //11:50:00

Upvotes: 3

jspit
jspit

Reputation: 7703

If you want to convert from one time zone to another regardless of server settings and Yii2 settings, use DateTime and do that

$localTime = '17:20';
$dt = date_create($localTime, new DateTimeZone('Asia/Kolkata'))
  ->setTimeZone(new DateTimeZone('UTC'))
;
echo $dt->format("H:i:s"); //11:50:00

However, if Asia/Kolkata is your correct local time zone you would like to work with within Yii2, then set it in the configuration as well.

Upvotes: 1

Related Questions