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.?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With