Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php Set TimeZone

I have following code:

$date = new DateTime(date("Y-m-d H:i:s"), new DateTimeZone('Asia/Karachi'));  
echo $date->format('Y-m-d H:i:s');

Output: 2015-08-26 17:46:05
Actual Result should be: 2015-08-26 13:46:05

How to set the time zone, so that actual output comes?

like image 476
Sobia Safdar Avatar asked Dec 24 '22 15:12

Sobia Safdar


1 Answers

Method 1 (Editing Php.ini)

1) Open your php.ini file

2) Add the following line of code to top of your php.ini file

date.timezone = "Asia/Karachi"

3) Restart php.

Note: You can find a list of timezone here: http://php.net/manual/en/timezones.asia.php

Method 2 (Using date_default_timezone_set)

In case you don't have access to the php.ini file, you can use date_default_timezone_set("Asia/Karachi"). Just put it in the entry point of your application.

http://php.net/manual/en/function.date-default-timezone-set.php

like image 104
Slay Avatar answered Dec 27 '22 21:12

Slay