Correcting server time inaccuracy

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Antnee
Forum Newbie
Posts: 24
Joined: Fri Aug 27, 2004 7:04 am
Location: Nottingham

Correcting server time inaccuracy

Post by Antnee »

Hi guys. As my first post here I'd like to ask how on earth do you correct an inaccuracy of your servers time?

I am based in the UK and my host is in the US. My SQL server's time seems to be OK (excusing the 6 hour time difference) but if I echo date() I get a time that is 6 hours and 11 minutes off GMT.

All I need to do is make the date and time match the date and time here. My primary concern is that if I just add a few hours and minutes thet days will be incorrect during that 6 hour 11 minute period as the day/month etc roll over.

I then need to insert this date and time to a MySQL field.

Does that make any sense? I hope someone can help me, this has been driving me up the wall for ages!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

[php_man]date[/php_man]
Returns a string formatted according to the given format string using the given integer timestamp or the current local time
[php_man]gmdate[/php_man]
Identical to the date() function except that the time returned is Greenwich Mean Time (GMT). For example, when run in Finland (GMT +0200), the first line below prints "Jan 01 1998 00:00:00", while the second prints "Dec 31 1997 22:00:00".
Btw, if your MySQL column is of type TIMESTAMP you will need MySQL's FROM_UNIXTIME function
Antnee
Forum Newbie
Posts: 24
Joined: Fri Aug 27, 2004 7:04 am
Location: Nottingham

Post by Antnee »

Thanks but all you've done is referred me straight to the first places I looked but I cannot get PHP.NET's solutions to work properly.

As for the MySQL entries, they are DATETIME and they work perfectly and are nothing to worry about, I jyust mentioned them as a side note, I'd just ignore them if I were you! :lol:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Talk to your host and ask them to change their time server, as it's obviously not working.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Antnee
Forum Newbie
Posts: 24
Joined: Fri Aug 27, 2004 7:04 am
Location: Nottingham

Post by Antnee »

I thought about that, but I can't see a US host correcting the time to GMT for one UK client, can you? It's a shared server so I'm not the only one on it
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

True, but if they're 11 minutes off the hour, then their server time is incorrect. There is no timezone that is only 11 minutes :). If they corrected their time even locally, then you could correct the hour, and not need to worry about minutes.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Antnee
Forum Newbie
Posts: 24
Joined: Fri Aug 27, 2004 7:04 am
Location: Nottingham

Post by Antnee »

That is a fair point, but I still need to know how to correct the hours! I don't want to have to mess around with locales or anything like that, it's too much of a headache if it gets moved to another server with a different OS, I just want something simple to correct the time myself
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Hmm, look at [php_man]gmdate[/php_man]() maybe?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Antnee
Forum Newbie
Posts: 24
Joined: Fri Aug 27, 2004 7:04 am
Location: Nottingham

Post by Antnee »

Looked at it already, and date() and mkdate(), I can't get any of them to work properly though, that's why I need help!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Correcting server time inaccuracy

Post by pickle »

Antnee wrote:My primary concern is that if I just add a few hours and minutes thet days will be incorrect during that 6 hour 11 minute period as the day/month etc roll over.
It will be incorrect for the server, but will be correct for your timezone. Adding 6 hours to whatever date() gives you will certainly give the wrong date and time for the server. If your interested in making it correct for your timezone though, adding 6 hours should work.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

For Indian Standard Time I add the difference.

Code: Select all

$today=mysql_fetch_assoc(mysql_query("SELECT DATE_FORMAT(NOW()+INTERVAL '09:30' HOUR_MINUTE,'%W, %D %M') AS IST"));
Antnee
Forum Newbie
Posts: 24
Joined: Fri Aug 27, 2004 7:04 am
Location: Nottingham

Post by Antnee »

OK, I did this:

Code: Select all

$today=mysql_fetch_assoc(mysql_query("SELECT DATE_FORMAT(NOW()+INTERVAL '06:00' HOUR_MINUTE,'%W, %D %M') AS GMT")); 
echo $today;
and I got this back:

Code: Select all

Array
If I type the command directly into MySQL I get this:

Code: Select all

Saturday, 28th August
Any ideas? I'm starting to think this just isn't going to work for me!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

echo $today['GMT'];
Antnee
Forum Newbie
Posts: 24
Joined: Fri Aug 27, 2004 7:04 am
Location: Nottingham

Post by Antnee »

feyd wrote:echo $today['GMT'];
Just figured that! Thanks! (Newbies eh?)

Got the required result with

Code: Select all

$today=mysql_fetch_assoc(mysql_query("SELECT DATE_FORMAT(NOW()+INTERVAL '05:00' HOUR_MINUTE,' %W %D %M %Y at %H:%i:%s') as GMT")); 
echo $today['GMT'];
Post Reply