Code Snippet: Fix date() when your server time is wrong

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Code Snippet: Fix date() when your server time is wrong

Post by Josh1billion »

(I have posted this here so that I can link it as an article on news.devnetwork.net - click to see the article there)

What do you do when your website is hosted on a server whose time is wrong, by an hour, by a minute, or even by a few seconds (if the seconds bother you..)? Or what if your host is just in a different time zone, and you want all date() queries to be returned in YOUR time zone, not theirs?

All you need to do is use this replacement for date(). I recommend putting it in a time.php file, and then including that file in all of your scripts. Any time you would make a call to date(), make a call to time_Date() instead. The calling syntax is exactly the same as date(), so if you want to make a transition from date() to time_Date() in all of your code, all you need to do is do a replace of "date(" with "time_Date(" in all of your files.

Whenever you need to change the time offset, just hard-code the $time_offset value in the function, and voila, all of your date/time queries will reflect the change. Just as you would with date(), you can also send in a timestamp as the second argument, and it will return the date/time for that timestamp (without offsetting it, obviously).

Optional: If you want to make the time offset dynamic (example: for the user to choose a time zone), you can also easily modify the code to meet those needs as well (I would recommend having a separate function if you choose to do that, though, and have that function make a call to this function, that way you can still use this function to correct the server time).

Code: Select all

function time_Date($format, $timestamp=false)
{ // this function should be used in all cases where date() would otherwise be used
	// Time offests are important if the server's time (or time zone) is wrong.
	
	$time_offset = 0; // setting for a perfect server (one that doesn't actually need this function).  Change the 0 to a different value, depending on how wrong the server's time is.  This is measured in seconds, not minutes/hours.

// Examples:
// Change 0 to -21600 for a server which is 6 hours too fast
// Change 0 to 3600 for a server which is an hour too slow
// Change 0 to -1800 for a server which is half an hour too fast
	
	if ($timestamp ==  false) // if timestamp is not passed..
		return date("$format", date("U") + $time_offset);
	else
		return date("$format", $timestamp);
}
edit: oops, I should have posted this in the Tutorials section
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 »

Actually, you should have posted this in the Code Snippets forum - I've moved it there.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

Oops, I didn't know this forum existed either. :P
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

...but code must pass the critique first before it is allowed into Code Snippets.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Quick question, why did you use date("U") instead of time()?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

hmm.... it's so simple. the only critique I can come up with is that the function name doesn't describe what it does. I'd called it date_offset() or something similar.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The only comment I have is the function isn't flexible. It corrects server time to one and only one offset. This is fine if all I wanted to do is show the server's time according to some locale, but what if I want to use the user's time too? It should be possible to set the offset at will.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

feyd wrote:The only comment I have is the function isn't flexible. It corrects server time to one and only one offset. This is fine if all I wanted to do is show the server's time according to some locale, but what if I want to use the user's time too? It should be possible to set the offset at will.
Sort of reminds me of this function i made a long time ago.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply