PHP: How to set timezone?

PHP

I have a linux ubuntu server and I want to change the default timezone for my PHP scripts. I tried to change the server timezone but this did not effect the timezone configured in PHP. I've checked the current timezone on my PHP scripts and it return “Europe/London”. 

echo date_default_timezone_get(); // returns "Europe/Longon".

Thanks in advance!
Ummi

Command-line interface (CLI) Server Question created: 2020-08-18 12:17 Ummi

9

You can set the timezone on your PHP script. In that way you can change the timezone on runtime. There is a PHP function which allows you to do this:

date_default_timezone_set (string $timezoneID); 

In your case you need to set the timezone like this:

date_default_timezone_set("Europe/London"); 

You can check / validate the time and timezone like this:

echo date_default_timezone_get();
var_dump(new DateTime());
date_default_timezone_set ("Europe/London");
echo date_default_timezone_get();
var_dump(new DateTime());
answered 2020-10-10 04:38 feedMeNow