Jump to content

getting users local date & timestamp


1kat

Recommended Posts

First, let me apologize if this question has been asked already on this forum.  I did search and was not able to find an answer.  I did search the web and found this topic but never a clear answer.

 

I need to get the users date & timestamp.  I know that php gets the servers timestamp when requested but I need the users local timestamp since users can log in from pretty much anywhere in the world. 

 

As soon as a specific page is opened i need the local date & timestamp of the user so that I can save it to a txt file on the server.  Please help.

Link to comment
https://forums.phpfreaks.com/topic/173186-getting-users-local-date-timestamp/
Share on other sites

If you get the user's IP address (I think it should be in the $_SERVER array, I can't remember what the specific param name is though), then you could pull the local server time, do a geo-lookup for what timezone the user's IP is in, then do a simple calculation to get from the server time zone to the users time zone.  But that will only work right for people who aren't behind a proxy in another location...

Thanks for the reply silver316 and bizcatalyst.

 

bizcatalyst - This sounds like a good idea.  After doing some research on it I found that this could often be inaccurate due to proxies, dst, etc.  Also I might have to pay to use a service that converts the ip to a timezone.

 

silver316 - My original goal was to use the users local pc time and date but did not find a way to pass the info from js to php in a way that would work for me.  Ajax sounds like it could help.  I will start researching how to get this done as I have never used ajax before.  If you have any links or example code I would really appreciate it. 

 

Thanks again.

 

Can ajax communicate with the server without any user interaction?  For example, without the user having to press a button on the site or scroll over anything.

 

I need as soon as the page opens for ajax to run and transfer a javascript variable to a php variable?

Is this possible?

If you get the user's IP address (I think it should be in the $_SERVER array, I can't remember what the specific param name is though), then you could pull the local server time, do a geo-lookup for what timezone the user's IP is in, then do a simple calculation to get from the server time zone to the users time zone.  But that will only work right for people who aren't behind a proxy in another location...

IP address isn't particularly useful: many ISPs use proxy servers, so you'd get the IP address of the proxy server rather than the user; and unless GeoIP gives you state/city as well as country, you'd have problems with countries that span more than one time zone. You'd also have to allow for daylight saving, which varies from country to country, and between the northern and southern hemispheres.

If you can resolve all those problems, IP address might give the correct time; but it's far easier and less error-prone to use javascript.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.