Page 1 of 1

Correcting server time inaccuracy

Posted: Fri Aug 27, 2004 7:04 am
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!

Posted: Fri Aug 27, 2004 8:37 am
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

Posted: Fri Aug 27, 2004 9:07 am
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:

Posted: Fri Aug 27, 2004 9:23 am
by pickle
Talk to your host and ask them to change their time server, as it's obviously not working.

Posted: Fri Aug 27, 2004 9:51 am
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

Posted: Fri Aug 27, 2004 9:56 am
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.

Posted: Fri Aug 27, 2004 10:01 am
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

Posted: Fri Aug 27, 2004 11:46 am
by pickle
Hmm, look at [php_man]gmdate[/php_man]() maybe?

Posted: Fri Aug 27, 2004 12:00 pm
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!

Re: Correcting server time inaccuracy

Posted: Fri Aug 27, 2004 12:30 pm
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.

Posted: Fri Aug 27, 2004 12:37 pm
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"));

Posted: Fri Aug 27, 2004 6:29 pm
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!

Posted: Fri Aug 27, 2004 6:35 pm
by feyd
echo $today['GMT'];

Posted: Fri Aug 27, 2004 6:41 pm
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'];