My Account

how to convert default date timezone in php

0 votes
9,683 views
asked in Web Development by Shyam
retagged by Bhartesh

I am using date function in PHP and storing time into database

Somehow its inserting US time zone and I want india time zone.

Using data function like this :

$today=date('Y-m-d h:i:s');


Please help me!

commented by Kaba N'faly
You can also use .htaccess file (at the root of your project) in which you will set the timezone as follow:
date.timezone = "Europe/Paris"
commented by Alex
You must be consistent across your code. Keep in mind you can have at least three different settings: the ini_set in php which affects date() functions, the MySQL setting which affects "SELECT NOW()" and the third one, the server time, which will have an impact on every other thing that is running on your server, and I mean external logs and cgi execs. Best practice: keep everything UTC and use a powerful date library such as Carbon (in php) and moment.js in JavaScript. You cans easily find them both on GitHub.

1 Answer

+2 votes
answered by jack (1,440 points)
edited by Bhartesh
 
Best answer
String date_default_timezone_get ( void )

In order of preference, this function returns the default timezone by:

  • Reading the timezone set using the date_default_timezone_set() function (if any)

  • Prior to PHP 5.4.0 only: Reading the TZ environment variable (if non empty)

  • Reading the value of the date.timezone ini option (if set)

  • Prior to PHP 5.4.0 only: Querying the host operating system (if supported and allowed by the OS). This uses an algorithm that has to guess the timezone. This is by no means going to work correctly for every situation. A warning is shown when this stage is reached. Do not rely on it to be guessed correctly, and set date.timezoneto the correct timezone instead.

If none of the above succeed, date_default_timezone_get() will return a default timezone of UTC.

Date function in PHP returns formatted date string ,

For India time zone you have to set

 

date_default_timezone_set("Asia/Kolkata"); 

$today = date('Y-m-d h:i:s a');
 

Now you would get date with India time zone. 

Related Questions

+2 votes
3 answers 255 views
+2 votes
1 answer 116 views
+4 votes
1 answer 584 views
+6 votes
3 answers 6,095 views
+6 votes
2 answers 1,120 views
0 votes
1 answer 30 views
+2 votes
1 answer 115 views
0 votes
1 answer 92 views