[PHP] Dates and different time zones

2007-04-10 Thread Niklas Karlsson
Hello,

 

I am creating an international site that is going to get used in both
American and Europe. And I wonder how to handle the different time zones,
because I want the date and time to be correct after the place your using
the application.

 

I now have the date_default_timezone_set() to Europe/Stockholm, and all
dates that is saved into the database is timestamp with time zone (using
PostgreSQL).

 

So a wonder who to handle different time zones? Have any of your experience
of this?

 

Cheers

Niklas Karlsson 



RE: [PHP] Dates and different time zones

2007-04-10 Thread Jay Blanchard
[snip]
So a wonder who to handle different time zones? Have any of your
experience
of this?
[/snip]

The server lives in one time zone and therefore cannot handle multiples
unless there is some new widget available for this. There are a couple
of methods available;

1. Use JavaScript to capture 'local' (client-side) time to a PHP
variable.
2. Use time zone offsets for ranges of IP addresses known to be in each
time zone. 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Dates and different time zones

2007-04-10 Thread Richard Davey

Niklas Karlsson wrote:


I am creating an international site that is going to get used in both
American and Europe. And I wonder how to handle the different time zones,
because I want the date and time to be correct after the place your using
the application.

I now have the date_default_timezone_set() to Europe/Stockholm, and all
dates that is saved into the database is timestamp with time zone (using
PostgreSQL).

So a wonder who to handle different time zones? Have any of your experience
of this?


Store all of your dates as GMT. Perform all date based calculations 
around GMT also, and then offset the values for localised display only.


This way you only need to store the GMT offsets for each user, i.e. 
GMT+1 or GMT-8 when it comes to displaying the dates to them. The trick 
is to use a constant base date for all data, and only being the user 
timezones into play when needed.


Cheers,

Rich
--
Zend Certified Engineer
http://www.corephp.co.uk

Never trust a computer you can't throw out of a window

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Dates and different time zones

2007-04-10 Thread Satyam


- Original Message - 
From: Richard Davey [EMAIL PROTECTED]

To: php-general@lists.php.net
Sent: Tuesday, April 10, 2007 3:43 PM
Subject: Re: [PHP] Dates and different time zones



Niklas Karlsson wrote:


I am creating an international site that is going to get used in both
American and Europe. And I wonder how to handle the different time zones,
because I want the date and time to be correct after the place your using
the application.

I now have the date_default_timezone_set() to Europe/Stockholm, and all
dates that is saved into the database is timestamp with time zone (using
PostgreSQL).

So a wonder who to handle different time zones? Have any of your 
experience

of this?


Store all of your dates as GMT. Perform all date based calculations around 
GMT also, and then offset the values for localised display only.


This way you only need to store the GMT offsets for each user, i.e. GMT+1 
or GMT-8 when it comes to displaying the dates to them. The trick is to 
use a constant base date for all data, and only being the user timezones 
into play when needed.




Actually, I find that it is better not to bother storing anything for the 
user at all.  At the first chance, get some JavaScript to read the local 
time of the client machine and send it back to the server, either with the 
login data, using some AJAX or along with any link the user might click on 
the welcome screen, for example, the language choice.  Then use the offset 
from his local time to the server time for every time information, substract 
it from any time information you read from them, add it to anything you send 
them.  This works whether the user is registered or not, whether he/she 
travels or remains in the same time zone and spares you the trouble of 
keeping your IP to country to timezone table updated.  It assumes that the 
user updates the time zone on his/her machine and if he doesn't it means she 
doesn't care, so why should you. (some travellers prefer to keep their 
portable machines set to their home-base time zone)


Satyam


Cheers,

Rich
--
Zend Certified Engineer
http://www.corephp.co.uk

Never trust a computer you can't throw out of a window

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 269.0.0/754 - Release Date: 09/04/2007 
22:59





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Dates and different time zones

2007-04-10 Thread Lester Caine

Satyam wrote:
Store all of your dates as GMT. Perform all date based calculations 
around GMT also, and then offset the values for localised display only.


This way you only need to store the GMT offsets for each user, i.e. 
GMT+1 or GMT-8 when it comes to displaying the dates to them. The 
trick is to use a constant base date for all data, and only being the 
user timezones into play when needed.


Actually, I find that it is better not to bother storing anything for 
the user at all.  At the first chance, get some JavaScript to read the 
local time of the client machine and send it back to the server, either 
with the login data, using some AJAX or along with any link the user 
might click on the welcome screen, for example, the language choice.  
Then use the offset from his local time to the server time for every 
time information, substract it from any time information you read from 
them, add it to anything you send them.  This works whether the user is 
registered or not, whether he/she travels or remains in the same time 
zone and spares you the trouble of keeping your IP to country to 
timezone table updated.  It assumes that the user updates the time zone 
on his/her machine and if he doesn't it means she doesn't care, so why 
should you. (some travellers prefer to keep their portable machines set 
to their home-base time zone)


Of cause the major fault with this is that it can only display the CURRENT 
time offset. You *ALSO* need the users Daylight Saving Zone as well. This has 
been giving us great fun since the winter dates and times need a different 
offset to the summer ones. Something that simplistic browser time offset does 
not supply. :(


The only way to get this working properly at present is to get the user to set 
their time/daylight settings in their profile, and then you can provide the 
correct offset for all days on a calendar. Remember that for users WITH a 
daylight saving offset, one day each year has 23 hours and one 25 hours ;)


--
Lester Caine - G8HFL
-
Contact - http://home.lsces.co.uk/lsces/wiki/?page=contact
L.S.Caine Electronic Services - http://home.lsces.co.uk
MEDW - http://home.lsces.co.uk/ModelEngineersDigitalWorkshop/
Treasurer - Firebird Foundation Inc. - http://www.firebirdsql.org/index.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Dates and different time zones

2007-04-10 Thread Satyam


- Original Message - 
From: Lester Caine [EMAIL PROTECTED]

To: php-general@lists.php.net
Sent: Tuesday, April 10, 2007 4:37 PM
Subject: Re: [PHP] Dates and different time zones



Satyam wrote:
Store all of your dates as GMT. Perform all date based calculations 
around GMT also, and then offset the values for localised display only.


This way you only need to store the GMT offsets for each user, i.e. 
GMT+1 or GMT-8 when it comes to displaying the dates to them. The trick 
is to use a constant base date for all data, and only being the user 
timezones into play when needed.


Actually, I find that it is better not to bother storing anything for the 
user at all.  At the first chance, get some JavaScript to read the local 
time of the client machine and send it back to the server, either with 
the login data, using some AJAX or along with any link the user might 
click on the welcome screen, for example, the language choice.  Then use 
the offset from his local time to the server time for every time 
information, substract it from any time information you read from them, 
add it to anything you send them.  This works whether the user is 
registered or not, whether he/she travels or remains in the same time 
zone and spares you the trouble of keeping your IP to country to timezone 
table updated.  It assumes that the user updates the time zone on his/her 
machine and if he doesn't it means she doesn't care, so why should you. 
(some travellers prefer to keep their portable machines set to their 
home-base time zone)


Of cause the major fault with this is that it can only display the CURRENT 
time offset. You *ALSO* need the users Daylight Saving Zone as well. This 
has been giving us great fun since the winter dates and times need a 
different offset to the summer ones. Something that simplistic browser 
time offset does not supply. :(




The point is that you don't store the time offset in any user profile or 
anywhere but a session variable, which you keep for the duration of the 
session so it lasts only while the user is connected, whether logged in or 
not, profile or not.  Next time he/she connects you get the new offset.  If 
the time has changed due to daylight savings or the user travelling 
elsewhere, you'll get a new offset.


The only time it fails is if the user is connected while the time switch is 
happening, but so will most of the clocks, watches and whatever is on at the 
time.


Satyam


The only way to get this working properly at present is to get the user to 
set their time/daylight settings in their profile, and then you can 
provide the correct offset for all days on a calendar. Remember that for 
users WITH a daylight saving offset, one day each year has 23 hours and 
one 25 hours ;)


--
Lester Caine - G8HFL
-
Contact - http://home.lsces.co.uk/lsces/wiki/?page=contact
L.S.Caine Electronic Services - http://home.lsces.co.uk
MEDW - http://home.lsces.co.uk/ModelEngineersDigitalWorkshop/
Treasurer - Firebird Foundation Inc. - 
http://www.firebirdsql.org/index.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 269.0.0/754 - Release Date: 09/04/2007 
22:59





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Dates and different time zones

2007-04-10 Thread Niklas Karlsson
Thanks for good answers.
Okay, so I should set the default time zone to central GMT time, and then
save GMT offset for every user. This sounds realistic, because I don't think
that I need to correct the time for users that doesn't login.

So, if I now have the GMT offset for every user, how do I display the right
date? Can someone please show some phpcode? I would be very grateful if
someone could do that.

Satyam wrote:
 Store all of your dates as GMT. Perform all date based calculations 
 around GMT also, and then offset the values for localised display only.

 This way you only need to store the GMT offsets for each user, i.e. 
 GMT+1 or GMT-8 when it comes to displaying the dates to them. The 
 trick is to use a constant base date for all data, and only being the 
 user timezones into play when needed.

 Actually, I find that it is better not to bother storing anything for 
 the user at all.  At the first chance, get some JavaScript to read the 
 local time of the client machine and send it back to the server, either 
 with the login data, using some AJAX or along with any link the user 
 might click on the welcome screen, for example, the language choice.  
 Then use the offset from his local time to the server time for every 
 time information, substract it from any time information you read from 
 them, add it to anything you send them.  This works whether the user is 
 registered or not, whether he/she travels or remains in the same time 
 zone and spares you the trouble of keeping your IP to country to 
 timezone table updated.  It assumes that the user updates the time zone 
 on his/her machine and if he doesn't it means she doesn't care, so why 
 should you. (some travellers prefer to keep their portable machines set 
 to their home-base time zone)

Of cause the major fault with this is that it can only display the CURRENT 
time offset. You *ALSO* need the users Daylight Saving Zone as well. This
has 
been giving us great fun since the winter dates and times need a different 
offset to the summer ones. Something that simplistic browser time offset
does 
not supply. :(

The only way to get this working properly at present is to get the user to
set 
their time/daylight settings in their profile, and then you can provide the

correct offset for all days on a calendar. Remember that for users WITH a 
daylight saving offset, one day each year has 23 hours and one 25 hours ;)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Dates and different time zones

2007-04-10 Thread Richard Davey

Niklas Karlsson wrote:


Okay, so I should set the default time zone to central GMT time, and then
save GMT offset for every user. This sounds realistic, because I don't think
that I need to correct the time for users that doesn't login.

So, if I now have the GMT offset for every user, how do I display the right
date? Can someone please show some phpcode? I would be very grateful if
someone could do that.


It depends where the dates are coming from. My guess would be a 
database, and if so why not let MySQL do the work for you?


SELECT date_created, DATE_SUB(date_created, INTERVAL 8 HOUR) AS 
offset_date from your_table


The above assumes the users offset is -8 hours. Use DATE_ADD for GMT + 
values.


Then you'll have both values in PHP - the actual original GMT date the 
date was created, and an offset date you can use for display.


Or of course you can pluck just the GMT date out and start using PHP 
functions to convert to local timezones, etc. But if the shoe fits ...


Cheers,

Rich
--
Zend Certified Engineer
http://www.corephp.co.uk

Never trust a computer you can't throw out of a window

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Dates and different time zones

2007-04-10 Thread Lester Caine

Satyam wrote:

Of cause the major fault with this is that it can only display the 
CURRENT time offset. You *ALSO* need the users Daylight Saving Zone as 
well. This has been giving us great fun since the winter dates and 
times need a different offset to the summer ones. Something that 
simplistic browser time offset does not supply. :(


The point is that you don't store the time offset in any user profile or 
anywhere but a session variable, which you keep for the duration of the 
session so it lasts only while the user is connected, whether logged in 
or not, profile or not.  Next time he/she connects you get the new 
offset.  If the time has changed due to daylight savings or the user 
travelling elsewhere, you'll get a new offset.


The only time it fails is if the user is connected while the time switch 
is happening, but so will most of the clocks, watches and whatever is on 
at the time.


Please read what I wrote.

The time offset from the browser is only of use to map CURRENT time. It is no 
use to display dates and times stored in the database that are reliant on the 
daylight saving offset. If TOMORROW is after the change in daylight saving, 
then the browser offset will not give you the right offset for tomorrows. The 
problem is convincing people that there *IS* a real problem, and trying to 
display the correct times JUST from a timezone offset is wrong for at least 
half of the year! You need to know that the time is changing tonight so that 
you can display tomorrows calendar correctly?


--
Lester Caine - G8HFL
-
Contact - http://home.lsces.co.uk/lsces/wiki/?page=contact
L.S.Caine Electronic Services - http://home.lsces.co.uk
MEDW - http://home.lsces.co.uk/ModelEngineersDigitalWorkshop/
Treasurer - Firebird Foundation Inc. - http://www.firebirdsql.org/index.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Dates and different time zones

2007-04-10 Thread Satyam


- Original Message - 
From: Niklas Karlsson [EMAIL PROTECTED]

To: php-general@lists.php.net
Sent: Tuesday, April 10, 2007 5:32 PM
Subject: RE: [PHP] Dates and different time zones



Thanks for good answers.
Okay, so I should set the default time zone to central GMT time, and then
save GMT offset for every user. This sounds realistic, because I don't 
think

that I need to correct the time for users that doesn't login.



Well, that was my whole point.  If you store the GMT offsets for each user 
at a certain time in the year, they will turn invalid when they switch from 
daylight savings to no savings. Not all countries use daylight savings and 
even those that do don't switch at the same time. Furthermore, northern and 
southern hemispheres switch in opposite directions, if and when they do.  If 
your users have to log in, that is the perfect time to read their current 
local time from the browser and keep it just for the session.  Don't store 
it permanently.


In PHP dates are stored as seconds from an arbitrary zero set at Jan 1st, 
1970, so does MySql with date/time values though it shows them formatted but 
you can use the UNIX_TIMESTAMP function to get the actual timestamp. 
JavaScript uses milliseconds from the same base date.  The rest is plain 
arithmetic.


Satyam


So, if I now have the GMT offset for every user, how do I display the 
right

date? Can someone please show some phpcode? I would be very grateful if
someone could do that.


Satyam wrote:
Store all of your dates as GMT. Perform all date based calculations
around GMT also, and then offset the values for localised display only.

This way you only need to store the GMT offsets for each user, i.e.
GMT+1 or GMT-8 when it comes to displaying the dates to them. The
trick is to use a constant base date for all data, and only being the
user timezones into play when needed.


Actually, I find that it is better not to bother storing anything for
the user at all.  At the first chance, get some JavaScript to read the
local time of the client machine and send it back to the server, either
with the login data, using some AJAX or along with any link the user
might click on the welcome screen, for example, the language choice.
Then use the offset from his local time to the server time for every
time information, substract it from any time information you read from
them, add it to anything you send them.  This works whether the user is
registered or not, whether he/she travels or remains in the same time
zone and spares you the trouble of keeping your IP to country to
timezone table updated.  It assumes that the user updates the time zone
on his/her machine and if he doesn't it means she doesn't care, so why
should you. (some travellers prefer to keep their portable machines set
to their home-base time zone)



Of cause the major fault with this is that it can only display the CURRENT
time offset. You *ALSO* need the users Daylight Saving Zone as well. This
has
been giving us great fun since the winter dates and times need a different
offset to the summer ones. Something that simplistic browser time offset
does
not supply. :(



The only way to get this working properly at present is to get the user to
set
their time/daylight settings in their profile, and then you can provide 
the



correct offset for all days on a calendar. Remember that for users WITH a
daylight saving offset, one day each year has 23 hours and one 25 hours ;)


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 269.0.0/754 - Release Date: 09/04/2007 
22:59





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Dates and different time zones

2007-04-10 Thread Satyam
You are totally right, I am sorry.  I would hate to miss my plane or train 
due to such mistake.


Satyam

- Original Message - 
From: Lester Caine [EMAIL PROTECTED]

To: php-general@lists.php.net
Sent: Tuesday, April 10, 2007 6:29 PM
Subject: Re: [PHP] Dates and different time zones



Satyam wrote:

Of cause the major fault with this is that it can only display the 
CURRENT time offset. You *ALSO* need the users Daylight Saving Zone as 
well. This has been giving us great fun since the winter dates and times 
need a different offset to the summer ones. Something that simplistic 
browser time offset does not supply. :(


The point is that you don't store the time offset in any user profile or 
anywhere but a session variable, which you keep for the duration of the 
session so it lasts only while the user is connected, whether logged in 
or not, profile or not.  Next time he/she connects you get the new 
offset.  If the time has changed due to daylight savings or the user 
travelling elsewhere, you'll get a new offset.


The only time it fails is if the user is connected while the time switch 
is happening, but so will most of the clocks, watches and whatever is on 
at the time.


Please read what I wrote.

The time offset from the browser is only of use to map CURRENT time. It is 
no use to display dates and times stored in the database that are reliant 
on the daylight saving offset. If TOMORROW is after the change in daylight 
saving, then the browser offset will not give you the right offset for 
tomorrows. The problem is convincing people that there *IS* a real 
problem, and trying to display the correct times JUST from a timezone 
offset is wrong for at least half of the year! You need to know that the 
time is changing tonight so that you can display tomorrows calendar 
correctly?


--
Lester Caine - G8HFL
-
Contact - http://home.lsces.co.uk/lsces/wiki/?page=contact
L.S.Caine Electronic Services - http://home.lsces.co.uk
MEDW - http://home.lsces.co.uk/ModelEngineersDigitalWorkshop/
Treasurer - Firebird Foundation Inc. - 
http://www.firebirdsql.org/index.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 269.0.0/754 - Release Date: 09/04/2007 
22:59





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Dates and different time zones

2007-04-10 Thread Richard Lynch
On Tue, April 10, 2007 11:49 am, Satyam wrote:
 You are totally right, I am sorry.  I would hate to miss my plane or
 train
 due to such mistake.

Can somebody with way more karma than me put in an RFC to just NUKE
daylight savings?

Pretty please?

Thanks!

Whatever alleged benefits there are, they cannot possibly outweigh the
drawbacks.

Time zones at :15 intervals are bad enough!

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Dates and different time zones

2007-04-10 Thread Richard Lynch
On Tue, April 10, 2007 11:38 am, Satyam wrote:
 In PHP dates are stored as seconds from an arbitrary zero set at Jan
 1st,
 1970, so does MySql with date/time values though it shows them
 formatted but

nitpick:
I don't think MySQL uses Unix timestamp internally, as it's quite
capable of storing a wider range than Unix timestamp.

I could be wrong, of course, depending on what data type you were
thinking of, and what version of MySQL, and what server settings are
in effect...

Databases have a bewildering plethora of date/time storage options. :-)

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php