Pages

Friday, February 25, 2011

Date and timezone issue solved



Hi....
I just faced an important problem when developing a website in PHP where the server

resides in one country and used in another.

The situation is this,
We have developed the website for indian users which resided in indian server
Recently we have changed this server to US.
The website is based on lots of date related functionalities  such as Calendar, Record

Created Date, Updated Date, History,etc,.
Since the server resides in US and the website really used and developed in India,

because of that, the results were shown with false data to the users of india wherever

date is displayed.

After sometime i found a solution

The solution to this would be using the timezone i.e

date_default_timezone_set("Asia/Calcutta") but even this will not solve all the issues

especially when you are inserting a date into the DB directly for eg using now()

"insert into tbloutgoing set sent_date=now()";

executing the above query will insert date and time of the US even you set the dafault

timezone but why, because the defualt_timezone is a function inside PHP not the server

function, the above query will execute inside phpmyadmin which is in US so the error,
This has two solutions,

The Easy way
1.You need to use timezone functionality
 Storing the date to a variable and inserting
    eg:
$mydate = date('Y-m-d');
"insert into tbloutgoing set sent_date='".$mydate."'";

The hard way
2.For this you need not use the timezone functionality.
 First you have to find the difference of time i.e US and India

Once you find it you can insert the date directly into the DB
   eg:
"insert into tbloutgoing set sent_date=now()+5.30";

Tips
1. include the timezone functionality.

Advantages of using easy way
1. You can shift the server to any country at anytime, and you need to change only the

timezone and in only one place.

Disadvantage of using the hard way
1.Shifting the server to another country.
   This would be a great disadvantage of using the hard way which may consume lot of

time, you have to find the difference and change it whereever you used date

functionality.


But i really wounder why this basic and important functionality doesnot exists  as a

default function in server.

No comments:

Post a Comment