Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get current date & time from visitor's machine or location

Status
Not open for further replies.

tyhand

Programmer
Jul 3, 2002
186
US
Hey all,

I'm using the date() function to get and echo the current date & time when a visitor comes to my site.

However, I noticed I'm not getting the desired results.

What I want to do is echo the current date and time on web page - so no matter where a visitor is located, his or her current date and time will be reflected.

How can I accomplish this?

Thanks all!

- Tyhand
 
Hi Feherke,

Thanks, but I need it in PHP. Also, I can format it to my liking.

- Tyhand
 
You could put a hidden field (e.g localdate) on the form to hold the date an after you have got it from Javascript. Write this value into localdate. When the form gets posted to the server php will see the localdate as it would any other variable.
 
Hey all,

Thanks for the suggestions.

Feherke, Ingresman... can I get a practical example using JS?

thanks!
 
Hi

I am thinking to send the OP to the AJAX forum... That because my first question is how the first visited page will display the date & time ? But better skip this option.

Anyway I would go with a minor modification of ingresman's suggestion : instead of [tt]hidden[/tt] [tt]form[/tt] [tt]input[/tt] I would use a cookie.

Feherke.
 
If memory serves me right if you use a Javascript cookie it doesn't get sent to the server along with other domain based cookies, which might not be a problem if it's client side only you need
 
Hi

You mean I do this in the browser :
JavaScript:
document.cookie='timezone='+new Date().getTimezoneOffset()
Then I do this on the server :
PHP:
<?php
print_r($_COOKIE);
?>
And I will not get this output :
Code:
Array
(
    [timezone] => -120
)
Well, I get it.

There is only one type of cookies : HTTP cookies. You can set them anywhere, you can read them anywhere. ( As long as the given resource has the right to access them. )

Feherke.
 
if you have registerred users, you can do a timediff based on the js time and the servertime and store this in mysql and then just putenv to change the timezone to reflect the difference.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top