simplfuzz
simplfuzz

Reputation: 12935

Unable to set timezone

I am unable to set timezone for my DateTime objects.
Here is my code :

$dt = DateTime::createFromFormat('U',time(),new DateTimeZone('Asia/Kolkata'));
print_r($dt->getTimeZone());

Here is the output :

DateTimeZone Object
(
)

I also tried putting these lines at the top (one at a time) - without any success:

date_default_timezone_set('Asia/Calcutta');
ini_set('date.timezone', 'Asia/Calcutta');
date_default_timezone_set('Asia/Kolkata');
ini_set('date.timezone', 'Asia/Kolkata');

Upvotes: 0

Views: 1763

Answers (2)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57690

This is because you specified UNIX timestamp in the parameter. See what php manual says.

The timezone parameter and the current timezone are ignored when the time parameter either contains a UNIX timestamp (e.g. 946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

What you are trying to do can be easily done by,

$dt = new DateTime("now", new DateTimeZone('Asia/Kolkata'));

If you have a variable that contains UNIX timestamp, first create a DateTime object with it. Then set the new TimeZone.

$dt = new DateTime("@$timestamp");
$dt->setTimezone( new DateTimeZone('Asia/Kolkata'));

http://codepad.viper-7.com/topBCR

Upvotes: 5

Rachel Gallen
Rachel Gallen

Reputation: 28583

try this

<?php

$dateTimeZoneAsia = new DateTimeZone("Asia/Kolkata");
$dateTimeAsia = new DateTime("now", $dateTimeZoneAsia);
$timeOffset = $dateTimeZoneAsia->getOffset($dateTimeAsia);
var_dump($timeOffset);
?> 

Upvotes: 0

Related Questions