Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP getting local date and time

How to get the local date and time in PHP.For example my timezone is Asia/Kolkata. My application has to add the date in my time zone to the database.I know the time can be found by adding 5.30 hrs to UTC time.But what about the date.After some googling I found that there is a DateTime object and it has a function setTimezone() to set the time zone.But after setting how can i get the current date in my time zone.?

like image 600
Jinu Joseph Daniel Avatar asked Feb 20 '23 18:02

Jinu Joseph Daniel


2 Answers

The localtime() function returns you all the data of the local time of the server. It has a lot of options so you can get it in different types of array (associative or numerically indexed). You can use also date() that use the local timezone of the server, too. You can change the timezone of the server with the date_default_timezone_set() function.


Learn more about the localtime() function and its parameters: http://php.net/manual/en/function.localtime.php or http://www.w3schools.com/php/func_date_localtime.asp

Learn more about the date() function and its parameters: http://php.net/manual/en/function.date.php

Learn more about the date_default_timezone_set() function and its parameters: http://www.php.net/manual/en/function.date-default-timezone-set.php

like image 66
Adrià Vilanova Avatar answered Feb 27 '23 19:02

Adrià Vilanova


You should rather use "date_default_timezone_set()" functions, once it is set, all the date time function will treat it as local timezone. I believe, you can also set it using php.ini

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

like image 33
FatalError Avatar answered Feb 27 '23 19:02

FatalError