0

PHP

$city = "Australia/NSW";
function get_timezone_abre()
{   
    global $city;
    $dateTime = new DateTime(); 
    $dateTime->setTimeZone(new DateTimeZone($city )); 
    return 'A'.$dateTime->format('T'); 
}

Result

AEST

Not sure either this will show daylight saving has started, can someone clarify this please?

Looked at this link, what-are-the-standard-timezone-abbreviations.

Can someone suggest a better way to do this (includes daylight saving)?

Add reading material

Australia DST timezone abbreviation incorrect when using date_default_timezone_set

  • Base on this, I assume my abbreviation EST is derived from using older php lib.
Community
  • 1
  • 1
roger
  • 1,225
  • 2
  • 17
  • 33
  • 1
    What's with the `A` at the start? If you're getting just `EST` as your timezone then it's not using the `Australia/NSW` timezone but rather some north american timezone (Eastern Standard Time). You should get either `AEST` or `AEDT` where one is standard time and the other is daylight time – Jonathan Apr 11 '17 at 02:26
  • 1
    Refer http://stackoverflow.com/questions/9736789/php-datetime-dst – GeorgeQ Apr 11 '17 at 02:29
  • As well why are you doing a global here? Shouldn't you pass in the timezone instead? If you want it to ALWAYS be `Australia/NSW` by default then you should either update your php.ini to set a default timezone, or add `date_default_timezone_set('Australia/NSW');` as the first line in your code if editing the php.ini isn't possible or undesired. – Jonathan Apr 11 '17 at 02:29
  • @Augwa What can I use to find why it doesn't use `AEST` or `AEDT`? Use of `global` is a habit dies hard haha, and also I have no idea where it is declared. – roger Apr 11 '17 at 02:39
  • probably because $city is something else. using globals reduces the predictability of your code. What does `$dateTime->format('e');` return? Probably `America/New_York` or some other EST timezone is my guess. – Jonathan Apr 11 '17 at 02:41
  • @Augwa `echo $dateTime->format('e');`, i get `Australia/NSW`. – roger Apr 11 '17 at 02:47
  • Then your output should be giving you `AAEST` then, not `AEST` – Jonathan Apr 11 '17 at 02:48
  • without the string `'A'`, my output is `EST`. I tried `$city = America/new_york;`, what I got is `EDT` and I assumed it is `EST` in winter. – roger Apr 11 '17 at 02:59

2 Answers2

2

Australia/NSW is not a valid timezone identifier. Valid timezone identifiers for Australia are:

[299] => Australia/Adelaide
[300] => Australia/Brisbane
[301] => Australia/Broken_Hill
[302] => Australia/Currie
[303] => Australia/Darwin
[304] => Australia/Eucla
[305] => Australia/Hobart
[306] => Australia/Lindeman
[307] => Australia/Lord_Howe
[308] => Australia/Melbourne
[309] => Australia/Perth
[310] => Australia/Sydney

You can find this list by using DateTimeZone::listIdentifiers() (see reference).

Australia/NSW is valid but this comment here says it was included for backwards compatibility and could be removed.

This code should do what you need, it considers the daylight savings changes too:

<?php
$city = "Australia/NSW";
$timezone = new DateTimeZone($city);
$date_time = new DateTime('now', $timezone);

echo $date_time->format('T') . PHP_EOL;
echo $date_time->modify('+6 months')->format('T');

This will output:

AEST
AEDT

Demo here: https://eval.in/772564

alistaircol
  • 1,433
  • 12
  • 22
2

Try this.

date_default_timezone_set('Europe/Sofia');
echo date_default_timezone_get(); // Europe/Sofia
echo ' => '.date('T'); // => EET
Hiren Makwana
  • 1,976
  • 2
  • 13
  • 28