How to get local timezone in PHP?

I use the date("D, m/d/Y h:i:s AT") function call date("D, m/d/Y h:i:s AT") to get the current time, but the return time is different from the time I get when I make a date in the linux bash command (local time is in CST and the php date function returns time in UTC ). I tried using $timezone = @system("date +%Z",$retval); to get the time zone in an attempt to set the local time zone using the date_default_timezone_set() function. But CST seems to be invalid timezone_identifier.

In short, I just need to get the same date and time as the local time.

+6
source share
3 answers

Use time zone from http://php.net/manual/en/timezones.php

So, to use CST:

 <?php date_default_timezone_set("America/Chicago"); 

If you have a time zone paragraph abbreviation, and not a time zone identifier, use:

 <?php date_default_timezone_set(timezone_name_from_abbr("CST")); 
+10
source

Although the system and PHP must be in the same time zone, read the date_default_timezone_set() documents to set the time zone manually.

Please note that CST is not a valid time zone setting. Find a valid time zone for your location.

+1
source

I think I found a better function: timezone_name_from_abbr()

0
source

Source: https://habr.com/ru/post/904553/


All Articles