0

I have a string which contains the date/time in it and i know its currently in Australia/Sydney timezone.

I now need to change it to GMT time.

I tried this, but its not changing the date:

$dateString = '2012-06-29 11:09:12'; // this is in Australia/Sydney timezone
$gmtDate = gmdate('Y-m-d H:i:s', strtotime($dateString));
print_r($gmtDate); // output is 2012-06-29 11:09:12

How do I subtract the offset somehow from gmt, sorry, getting a little confused.

George Brighton
  • 5,131
  • 9
  • 27
  • 36
user1067698
  • 167
  • 1
  • 1
  • 6

3 Answers3

4

Perhaps your web-server is not set up in the Australia/Sydney timezone ? The following answer will help you: how to convert php date formats to GMT and vice versa?

Here is what you can do:

$melbourne = new DateTimeZone('Australia/Melbourne');
$gmt = new DateTimeZone('GMT');

$date = new DateTime('2011-12-25 00:00:00', $melbourne);
$date->setTimezone($gmt);
echo $date->format('Y-m-d H:i:s');
// Output: 2011-12-24 13:00:00
// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.

echo '<br/>';

// Convert it back to Australia/Melbourne
$date->setTimezone($melbourne);
echo $date->format('Y-m-d H:i:s');
Community
  • 1
  • 1
jonjbar
  • 3,896
  • 1
  • 25
  • 46
3

Using ini_set() in your script to set the current timezone to e.g. the timezone that is used in Los Angeles, you would do this:

ini_set('date.timezone', 'America/Los_Angeles');

All subsequent requests to date and time functions would use America/Los Angeles as the timezone.

If you need to do this in your scripts, for example if the web server you are using is in a different timezone from the dates and times you wish to use in your scripts, then you should ensure the call is made at the very start of your scripts (or, if you can, move the setting out to an .htaccess file).

Sturm
  • 4,105
  • 3
  • 24
  • 39
1

PHP Support Timezone, check here:

date_default_timezone_set function List of Supporting Timezones

Ex:

date_default_timezone_set('Australia/Sydney');
echo date('Y-m-d H:i:s') . '<br/>';

date_default_timezone_set('Asia/Jakarta');
echo date('Y-m-d H:i:s') . "<br/>";
Joko Wandiro
  • 1,957
  • 1
  • 18
  • 28