PHP - How to determine timezone - timezone

PHP - How to determine timezone - timezone

Colleagues hello to all.

In today's article, we'll talk about how to find out what time zone is set on your site using the PHP programming language.

Knowing what time zone you have set on the site is a very important task, because all time values ​​displayed on your site and entered by the user will be based on this in the future. Sometimes there is a situation that the current time on the server does not match your current time zone or the time zone of the region that your site is targeting. We can call this built-in function to get the default time zone.

The PHP programming language has some easy ways to find out your current time zone, and I'll show you today.

 

date_default_timezone_get

In the first option, we will use the  date_default_timezone_get function. The date_default_timezone_get function returns the timezone that is set by default in the php.ini file.

php> date_default_timezone_get();

php_get_timezone

As a result, we get the time zone that we have set Europe/Kiev


 

DateTimeImmutable

In this example, we can use a php class called DateTimeImmutable. The  DateTimeImmutable class refers to classes that are responsible for date and time. We need to create an object of this class and then call a method of this class called getTimezone.

php> $timezone = new DateTimeImmutable();
php> $timezone->getTimezone();

php_get_timezone


 

DateTime

In the last example, we will use the php class DateTime. The DateTime class also belongs to the classes that are responsible for date and time. We need to create an object of this class and then call the method of this class getTimezone.

php> $timezone = new DateTime();
php> $timezone->getTimezone();

php_get_timezone


 

Thank you all, I hope that my article helped you in some way.

 

SIMILAR ARTICLES

PHP - How to remove spaces from the beginning and end of a string
PHP - How to remove spaces from the beginning and end of a string
WEB
Read more
PHP - date formatting. date() function.
PHP - date formatting. date() function.
WEB
Read more
PHP - Unix Timestamp Functions
PHP - Unix Timestamp Functions
WEB
Read more