954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Global time

Im creating a time-base site wherein the timestamp should rely on the global time
and not to my PC's time (because it can be out-dated somethimes).
How can I do that?

rakwel10
Junior Poster in Training
68 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Unless is specified by localtime(), time_zone cookies and so on, PHP works with the server time, not with client time, if you want to use GMT then use gmdate():

<?php 
echo gmdate('c'); 
echo date('c'); 
?>


http://www.php.net/manual/en/function.gmdate.php

cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

Thanks for the reply. How can it know the time in your location? The time in the east is different from the west.

rakwel10
Junior Poster in Training
68 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

If the user register to the website you can ask for the timezone location, at that point your script becomes:

<?php 
date_default_timezone_set('Europe/London'); # or America/New_York
echo gmdate('c') . "\n"; 
echo date('c') . "\n"; 
?>

- http://www.php.net/manual/en/function.date-default-timezone-set.php
- http://www.php.net/manual/en/timezones.php

You can also use javascript to get the datetime, send it to your server through ajax and try to figure the timezone. Probably you can also use IP Geolocation but I can't help you on this.

cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

thanks! :D

rakwel10
Junior Poster in Training
68 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

You're welcome :)

cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

Hello Cereal!

I have some concerns again...

I tried your code:

<?php 
date_default_timezone_set('GMT+8');
echo gmdate('c') . "\n"; 
echo date('c') . "\n"; 
?>


Yes it did show up some dates but the date is still machine date..

2011-29-08, 01.52.45 am //result on gmdate();
2011-08-29T01:52:45+00:00 //result on date();

The exact time now here is 10am.
Seems something is wrong..
What do you think?

rakwel10
Junior Poster in Training
68 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

i got it! :))

rakwel10
Junior Poster in Training
68 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

great ;D

cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: