0

The code below will not give me the time in the timezone I specify. Why

    $dateTimeStr = substr($this->statsGW->fetchLatestDateTime(), 0, -7);
    $defaultTZ = new DateTimeZone("Europe/Rome");
    date_default_timezone_set("Europe/Rome");
    date("H:i",strtotime($dateTimeStr));

Instead it keeps returning the UTC time that is stored in the database and is fetched with the first code.

idipous
  • 2,868
  • 3
  • 30
  • 45
  • The date is stored in the DB, the UTC time ? Than you cant change it with new DateTimeZone or set default timezone. You have to store the correct date in the DB or calculate it manually. – Webice Aug 15 '14 at 07:16
  • possible duplicate of [Timezone conversion in php](http://stackoverflow.com/questions/2505681/timezone-conversion-in-php) – Ed Heal Aug 15 '14 at 07:24
  • It is stored in UTC in the DB. I have tried to set the default timezone but it always returns the date as stored in the dB – idipous Aug 15 '14 at 07:38

1 Answers1

1

You need to specify default time zone before get the date.

date_default_timezone_set("Europe/Rome");
$today = getdate();
print_r($today);

You can access any attribute of date object using $today vairable.

manitaz
  • 1,181
  • 2
  • 9
  • 26
  • But the date is stored in the database and I retrieve it as string from it. Then I change it with strtotime and I have set the default_timezone_set before I do that but nothing. – idipous Aug 15 '14 at 07:39
  • Do you need to convert UTC time to Your declared time zone? If it is you can add or subtract time deference to UTC time. – manitaz Aug 15 '14 at 08:56
  • That is what I ended up doing but I was wondering why the solution I have posted does not work – idipous Aug 17 '14 at 18:54