Welcome to WebmasterWorld Guest from 107.21.175.43

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Getting correct times in PHP

Is there some way to get correct time zones?

     

Travholt

10:19 am on Sep 3, 2002 (gmt 0)

10+ Year Member



Here is my problem: I have a list of profiles for users of an online community. These people are all over the world, and I want to display their correct time as part of their profile. But this means I have to think about DST, which is kind of a floating factor.

So I wonder if there are functions in PHP which enable me to specify a rough location or time zone to get correct time, or if there's a server one can contact for that in some way.

Nick_W

10:33 am on Sep 3, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Have you looked at setlocale() ?

[php.net ]

It may be what you're after...

Nick

Travholt

10:55 am on Sep 3, 2002 (gmt 0)

10+ Year Member



Hm, yeah. I've been looking around a little more (I momentarily forgot how much useful info there is in the php.net user comments), and it seems there's no premade function or easy way to do it. But it seems like using locales is the way to go about it.

ergophobe

10:36 pm on Sep 4, 2002 (gmt 0)

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Just by chance I came across an article on exactly this topic (uses JS and PHP) so that you should get a correct time without any user input. Then the hard par was remembering where I had seen this question posted!

I think this will do it for you

[martin.f2o.org...]

Tom

PaulPaul

10:37 pm on Sep 4, 2002 (gmt 0)

10+ Year Member



On the off chance, you have a database connected to the site. You could always do a getdate().

ergophobe

3:04 am on Sep 5, 2002 (gmt 0)

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



PaulPaul,

He wants the date at the client site, not the date at his server, which is what the getdate() would give him

Tom

PaulPaul

3:26 am on Sep 5, 2002 (gmt 0)

10+ Year Member



hmmm, ergophobe you are right. I apologize :)

Well in that case, you can have the user select a time zone, and then still do a getdate() :) and add or subtract hours according to your time zone.

Lisa

6:39 am on Sep 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PaulPaul,
He can figure out user timezone already. The problem appears when there is day light savings change and the timezone changes in one location but not the others. There can be like a 2 week delays and some none at all. This is quite a difficult problem. Brett has spent many hours working on just this issue. But he used Perl. Thanks for the article ergophobe, I will come back here and search for if/when I need it. And I think I will. :)

ergophobe

12:10 am on Sep 6, 2002 (gmt 0)

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The method used in the article is to

- use JS to get the system time on the client computer
- create a PHP session
- keep track of the user and pass the user's local time as a session variable.

Of course, this won't work if the user has JS turned off or has the time incorrectly set on his computer (for example a laptop user who is travelling or someone who just doesn't care). So it should get the correct time for at least 80% of users.

Tom

martin

8:53 am on Sep 6, 2002 (gmt 0)

10+ Year Member



> it should get the correct time for at least 80% of users.

It probably is more than 80%, if I was flying a plane I wouldn't change my computer's timezone every time I cross a timezone line.
And if I did it would actually confuse me more.

/add
The DST is another problem as Windows messes it up for me, on NetBSD you have to edit the symbol table of your kernel, Linux seems to do it right.

ergophobe

5:41 pm on Sep 6, 2002 (gmt 0)

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month




It probably is more than 80%, if I was flying a plane I wouldn't change my computer's timezone every time I cross a timezone line.

Well, that's what I meant really (and I agree, the 80% was meant to be conservative - 10% with JS turned off and 10% traveling or simply having the wrong system time which would actually still give you about 81% total).

I also agree that most people who don't adjust to the time zone they're in (either travel or their OS doesn't auto adjust for DST) probably would just be confused by a page that showed the "correct" time for their location. However, it is true that formally the times shown would be incorrect for those folks.

Tom

martin

8:45 am on Sep 7, 2002 (gmt 0)

10+ Year Member



>However, it is true that formally the times shown would be incorrect for those folks.

Yes, but I don't see them visiting a page and adjusting it, so for them it will "always" be incorrect time zone.

PS. Windows time zones are really a mess, another thing M$ cares for much.

ergophobe

5:41 pm on Sep 9, 2002 (gmt 0)

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month




Yes, but I don't see them visiting a page and adjusting it, so for them it will "always" be incorrect time zone.

Huh? How so? It will be the system time on their local machine, which is what the script author wants. So if their system time is correct and they have javascript enabled, it *will* be correct.

It should only be incorrect if (the system time is incorrect) or (they have javascript disabled and they are in a different time zone than the default for the site).

Tom

martin

7:58 am on Sep 10, 2002 (gmt 0)

10+ Year Member



>if their system time is correct and they have javascript enabled, it *will* be correct.

Yes, that was the sole purpose of the code.

>It should only be incorrect if (the system time is incorrect) or (they have javascript disabled and they are in a different time zone than the default for the site).

In the last comment actually I wanted to explain that it is unlikely that a person who has his machine's time zone incorrect is going to go to a page where he is supposed to enter his time zone.

ergophobe

5:19 pm on Sep 10, 2002 (gmt 0)

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month




person who has his machine's time zone incorrect is going to go to a page where he is supposed to enter his time zone.

Oh sure. That's certainly right, but anyone who doesn't have his system time set correctly probably won't care that the times on the pages he's viewing are incorrect too, so I think you can discount those people.

Doh! I finally realized that the script I mentioned is Martin's script! I guess he knows how it works... we're just not getting our message across to each other. Anyway, it's a good script and should do what the original poster wants pretty well.

Tom

g1smd

10:28 pm on Sep 14, 2002 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month




Time Zones are VERY complicated, especially regarding DST which is a political decision and hence variable in application over the years. Some user communities have given up on the idea and have adopted UTC all over the world, all year round (Astronomers and Amateur Radio Operators to name two). Additionally, astronomers have adopted the ISO 8601 [RFC 3339] YYYY-MM-DD dates to avoid the problems with interpretation of other formats.

This site has good information about the changes to DST: [timeanddate.com...] .

martin

10:53 pm on Sep 14, 2002 (gmt 0)

10+ Year Member



>Some user communities have given up on the idea and have adopted UTC all over the world, all year round...

Yes, that's some. Most are not, they are using their local times with their own DST rules.

The scripts will get it right if the OS is set up correctly.

PS. I personally don't have anything against UTC.

martin

1:44 pm on Sep 19, 2002 (gmt 0)

10+ Year Member



What is the most commonly used time zone in USA? And its abbreviation of course... oh and the GMT offset.

I want to make it available for users without JavaScript and want to give an example.

g1smd

7:11 pm on Sep 19, 2002 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The US has several time zones:

Standard ... Daylight Savings
(Winter) Abb (Summer) Abb

UTC-0500 EST UTC-0400 EDT Eastern
UTC-0600 CST UTC-0500 CDT Central
UTC-0700 MST UTC-0600 MDT Mountain
UTC-0800 PST UTC-0700 PDT Pacific

However there is no International Standard for Time Zone Names, EST could be Eastern Standard Time (US), European Summer Time, or Eastern Standard Time (Australia).

It is best to use the +0430 and -0500 notation, because this instantly shows what the actual offset is. This notation does need all four digits as there a number of time zones that are 15, 30, or 45 minutes from being a full hour offset to UTC.

When calculating the Time elsewhere on the planet, don't forget that the date changes where the time passes through midnight. Also note that clocks in Britain run on GMT (UTC+0000) in Winter, but that in the Summer a clock on the wall in Britain, uses BST (British Summer Time) which is one hour ahead (UTC+0100). So, do not fall into the trap of calculating the offset from the time someone in Britain tells you. Refer directly to a UTC time source.

[edited by: g1smd at 7:37 pm (utc) on Sep. 19, 2002]

martin

7:22 pm on Sep 19, 2002 (gmt 0)

10+ Year Member



Yes, I know there are 3 time zones. I just want to give one example.

I wanted to know which one is most widely used.

g1smd

7:41 pm on Sep 19, 2002 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month




The one used depends on whether you live on the East coast, the West coast or somewhere in between. You can't pick one as being the typical time zone, without knowing where the person actually is.

martin

7:53 pm on Sep 19, 2002 (gmt 0)

10+ Year Member



Thanks for that info. I just wanted to know which time zone is valid for most people.

ergophobe

8:04 pm on Sep 19, 2002 (gmt 0)

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



There is no time zone that is valid for *most* people (50% or more). I would guess that the Eastern Time Zone includes the greatest population, followed by either Western or Central, with the lowest population in the Mountain Time Zone.

I would use Eastern, since those of us on the Left coast are more used to converting and having the wrong time zone than are people on the Right coast.

Tom

g1smd

8:27 pm on Sep 19, 2002 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Don't forget that some states do have Daylight Savings Time in the summer while others do not. In some states, some counties observe DST, while other counties within the same state do not. Too confusing.

You referred to 'GMT' in an earlier posting. Don't forget that term went obsolete back in 1971, when it was replaced by UTC. The term GMT is best avoided as it reflects the local time zone used in the UK only in the Winter. Clock time in the UK is currently not GMT; it is BST, which is UTC+0100.

martin

10:16 am on Sep 20, 2002 (gmt 0)

10+ Year Member



>Don't forget that term went obsolete back in 1971, when it was replaced by UTC. The term GMT is best avoided as it reflects the local time zone used in the UK only in the Winter. Clock time in the UK is currently not GMT; it is BST, which is UTC+0100.

I know that but I think GMT is more known by people than UTC.

The UK time now can be both explained as GTM +1:00 and UTC +1:00.

g1smd

9:20 pm on Sep 20, 2002 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month




>> The UK time, now, can be both explained as GMT +1:00 and UTC +1:00 <<

Yes, it _could_ be explained as GMT+1:00, but this tends to confuse people who are outside the UK. Someone in the US, perhaps, knows his time zone as -5 hrs. In Summer the Time at Greenwich is GMT+1, and this can therefore lead to the person in the US taking 5 hours off the time at Greenwich, rather than 5 hours off the GMT time. This is why the offset to UTC is used. It then becomes clear that the UK time zone isn't GMT for over 7 months of the year.

In effect the old way was to say that 'Greenwich = GMT+1' in Summer. For time zone calculations you should have been using GMT as the base, but people inadvertently use the 'Greenwich civil time' which in Summer is known as BST (British Summer Time) and therefore get an answer that is one hour wrong for a part of the year.

Visit Thailand

9:51 am on Sep 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just wanted to check if you have this :

$anyvariable = (date("d M Y H:m:s"));

How can I get it so that the time output adds 12 hours to the actual time?

martin

1:41 pm on Sep 26, 2002 (gmt 0)

10+ Year Member



Date has a second parameter which is the time you need if you don't want the current.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month