Timezone conversion help needed.

Status
Not open for further replies.

parkourmumbai

New Member
Messages
34
Reaction score
0
Points
0
Seeing as no one seems to notice my second issue in the other thread, I'm creating a new one here in the hopes that people will see and respond ti the relevant problem over here. I have also done a cursory search on the boards, and didn't find anything pertaining to my issue.

Simply put, I am a total noob when it comes to converting dates and times across timezones, even in the offline world. And I still don't even know what daylight saving really is, and how it is implemented, since I don't come from a daylight saving country.

I'm located in India, and from what I can make out, the x10 hosting servers are located in New York, USA.

In one of my forms, I have a feature that captures the current date and time (preferably of the timezone GMT +05:30, Indian Standard Time) at the time of hitting the submit button, and inserts it into a 'datetime' datatype field in the backend database, along with the other information.
Now since the x10 servers are located in the USA, I can't use the time() or the date() functions to capture the datetime values, as I would still need to work on the data captured to convert it to my timezone.

Could someone please help me with a function(s) to convert the datetime to reflect the accurate timezone in my country, or if there are built-in functions in php/mysql to accomplish this, by pointing me to those and how to use them, at the same time providing for the daylight saving adjustment that would be applied to the cossacks server for the specific months every year?

I'm being very specific here -
I want to capture the current date and time on the cossacks server and convert it to the current date and time as per the Indian Standard Time, which is GMT + 05:30 hours, and at the same, time make some provision for daylight savings, whenever in the year it is implemented on cossacks for a period of time, without having to recode when daylight savings is in effect on cossacks. Then I want to parse it in the correct format for entering a 'datetime' value in mysql, and pass it onto a parameterized query for inserting into the database.

I have already searched for this thing high and low on google, and couldn't find anything to help me.

Any help here would be appreciated. Thanks in advance.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
This area is a nightmare!

I have done a lot of work on it and finally came up with the best solution a month or so back..

http://forums.x10hosting.com/programming-help/78812-multiple-date-time-support-2.html

Couple of comments:

All dates should be stored as a single timezone - i.e. UTC.

All you do then is load the browsers timezone and echo this value according to their timezone.

If you store a date in the users timezone, things get very messy and you have to start converting backwards and forwards depending on the timezone difference.

The way I handled this was storing as an epoch timestamp, which by default is in UTC format.

When a user logs in (provided there is a session), you can take the timezone preference as a session variable (UK for example) and set it as an environment variable.

putenv("TZ=...echo from session user preferences

Because the environment is now thinking in local time rather than server time, all values returned from the UTC timestamp value are automatically converted to local time.

With this method, a user in the states could enter a date from their local time and you (in India) would see that value relative to the states.

Its the simplest solution, but it does mean messing with varchar strings rather than datetime strings (not really a big problem)

My site uses this method throughout (you'll see the benefits more easily when you log in "Demo" & "Password")

Let me know if this is a solution you could consider and I'll help you with the coding.


BTW. - daylight savings are taken care of automatically with some php are codes. For instance, the GB timezone automatically adjusts beteen winter and daylight saving without having to add or subtract an hour.
 
Last edited:

parkourmumbai

New Member
Messages
34
Reaction score
0
Points
0
Man, your other thread was getting too complicated, and my issue is simple.
But I appreciate your help all the same.

No sessions here, I just want to store user comments to a particular aspect of my site, and also store a date and time along with it.
How do I get the epoch UNIX timestamp?
The time() function returns the timestamp in the UNIX format, but draws the current timestamp from the server's date and time, which could be in a timezone other than UTC, and in my case EDT.

I'm not dealing with multiple timezones and user preferences. I just want to convert the server's timezone (EDT) to mine (IST) before storing the datetime value in the db. I'm not even storing the timezone values in the db, just the date and the time. When I retrieve it for displaying, I parse it according to my preference, and append the string ("IST" or "Indian Standard Time") to it.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Timezones are a complex subject!

How do I get the epoch UNIX timestamp?

Simple - just insert a time() value into a hidden field in your form.

PHP:
<input name="fieldnow" type="hidden" id="eveditedwhen" value="<?php echo time();?>" />

Then include $_POST['fieldnow'] into your insert statement.


The time() function returns the timestamp in the UNIX format, but draws the current timestamp from the server's date and time, which could be in a timezone other than UTC, and in my case EDT.

It shouldn't do. A timestamp by definition is the number of seconds passed since midnight Coordinated Universal Time (UTC) of Jan 1st 1970. This remains constant regardless of your timezone.

Only if you echo a value from date() do you get a server time. (from the states).

I'm not dealing with multiple timezones and user preferences. I just want to convert the server's timezone (EDT) to mine (IST) before storing the datetime value in the db. I'm not even storing the timezone values in the db, just the date and the time. When I retrieve it for displaying, I parse it according to my preference, and append the string ("IST" or "Indian Standard Time") to it.

You don't have to convert the server's timezone - honestly - this is a common misconception that complicates things.

At the top of your page, simply set your environment using this

PHP:
putenv ("TZ=IST");

You need an intermediate account for this function.

This doesn't convert anything yet - it just changes your environment to your timezone. It still doesn't alter any timestamp values being stored but it does change the way in which date() works.

You can now echo a date normally in your own timeformat preference from the epoch timestamp.

PHP:
echo date('d-m-Y H:i:s', $whatever)

Because your environment is thinking in IST, php automatically makes the required adjustment from UTC.

It only gets more complex when entering a date manually! (i.e. not from time()) - but I can also help with this if needed.

______________

Just remembered - your MySQL field to store epoch timestamp is varchar, NOT datetime.
 
Last edited:

parkourmumbai

New Member
Messages
34
Reaction score
0
Points
0
Thanks for your help, it really clears things up.

But I tried using the code you gave, and it still outputs date time values in the server's time.

PHP:
<?php
putenv ("TZ=IST");
$usrdate = time();
echo $usrdate;
echo "<br />";
echo date('d-m-Y H:i:s', $usrdate);
echo "<br />";
?>

This gives me an output -
Code:
1223728129
11-10-2008 08:28:49

While it is actually 05:58 (17:58) in my time. The time displayed above is in AM for New York, cossacks time.

I don't know why it's doing this man, it seems even though I got the intermediate php activated for me a few days ago, it is still not letting me use those functions. I've run phpinfo() on my hosting account, and it says all those functions are now accessible to me, but they still don't work. Possibly that's why it's still displaying the server's tiem instead of mine even after setting the putenv() to my timezone.

It only gets more complex when entering a date manually! (i.e. not from time()) - but I can also help with this if needed.
Thanks man, if I ever need to enter dates manually, and am stuck, I'll certainly remember you. :)

Just remembered - your MySQL field to store epoch timestamp is varchar, NOT datetime.
Are you sure?
Then I'll just have to change the corresponding data type in my backend. Thanks again.
 
Last edited:

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Lol - just figured out why...

IST is not a valid php timezone!!!

India has several timezones;

Indian/Reunion
Indian/Chagos
Indian/Antananarivo
Indian/Christmas
Indian/Cocos
Indian/Comoro
Indian/Kerguelen
Indian/Mahe
Indian/Maldives
Indian/Mauritius
Indian/Mayotte

Exactly as I've put - nothing wrong with your code.
 

mattura

Member
Messages
570
Reaction score
2
Points
18
ARGH!!
I spent a while messing about with this ages ago, to find that one line is ALL you need!!!

I use the following for the British Timezone:
PHP:
date_default_timezone_set('Europe/London');  //set British timezone:

I haven't tried, but 'IST' or the usual timezone code for your location should work.
Edit:
IST doesn't work, it has been deprecated.

Take a look here to choose your timezone (note that the timezone you want could be under Asia or India):
http://docs.php.net/manual/en/timezones.php

Timezone test:
PHP:
<?php
$tz=array("America/Los_Angeles","America/New_York","UTC","Europe/London","Asia/Calcutta","Indian/Maldives","Europe/Moscow");
$now=mktime();
foreach($tz as $v) {
 date_default_timezone_set($v);
 echo "Default Timezone: ".date_default_timezone_get()."<br/>";
 echo "The current time: ".date("H:i:s",$now)."<br/>";
}
?>
 
Last edited:

parkourmumbai

New Member
Messages
34
Reaction score
0
Points
0
Thanks mattura,
It worked.

But if 'IST' has been deprecated, then why does this code

PHP:
<?php
date_default_timezone_set('Asia/Calcutta');
$usrdate = time();
echo $usrdate;
echo "<br />";
echo date('d-m-Y H:i:s', $usrdate);
echo "<br />";
echo date('T', $usrdate);
echo "<br />";
?>
output this result?

Code:
1223829283
12-10-2008 22:04:43
IST


@freecrm,
Lol man, but ironically, none of them apply to me. None of those countries mentioned are located even anywhere close to mine, and I hadn't even heard of half the ones mentioned till now. :biggrin:
 

mattura

Member
Messages
570
Reaction score
2
Points
18
IST has been deprecated in the date_default_timezone_get/set function.
It is still a time zone!
The php people thought it was not very descriptive when setting the time zone to have short abbreviations like 'IST', 'GMT' and others, so they reworked it by continent, with a full city name.
 

sunils

New Member
Messages
2,266
Reaction score
0
Points
0
Its happy to know that your issue is resolved. All the best for your website.
/**************Thread Closed**************/
 
Status
Not open for further replies.
Top