1
echo (date('d-m-Y',strtotime("1997-02-27T00:00:00+08:00"))); 

The result of this is one day before. 26-02-1997

I have an API that brings time like the format above. How can I implement timezone to date function ?

aynber
  • 22,380
  • 8
  • 50
  • 63
  • Possible duplicate : https://stackoverflow.com/questions/29526129/php-date-showing-wrong-time-despite-the-timestamp-being-correct – Amine KOUIS Sep 04 '20 at 11:56

2 Answers2

0

You can change the default timezone with date_default_timezone_set(). The list of timezones for that is here.

For example:

date_default_timezone_set('Australia/Sydney');
echo (date('d-m-Y',strtotime("1997-02-27T00:00:00+08:00")));

My current timezone is 26-02-1997, this makes it Australian: 27-02-1997.

Morgosus
  • 807
  • 5
  • 18
  • Thanks Martin, that worked for me. And is there any way to get this timezone from client's location or browser dynamically? – Alperen Ozkan Sep 04 '20 at 11:59
  • I'd take a look at [this](https://stackoverflow.com/questions/16525617/how-to-detect-users-timezone).. you can find the rough idea of a timezone (well.. language) in the HTTP header's accepted language (en-US etc), so that's where I'd look if I didn't want to deal with too much JavaScript. – Morgosus Sep 04 '20 at 12:11
  • You can change it dynamically to your liking (or even use several time zones at the same time with `DateTime` objects) but you need to be aware that PHP is a server-side language. It doesn't need or interact with browser settings. – Álvaro González Sep 04 '20 at 13:45
0

Open the php.ini file and set this code date.timezone = "US/Central" to define definitely.

You can check if alright on section date using the command:

<?php phpinfo();
Tarsis Cruz
  • 95
  • 1
  • 7