PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Nov 13, 2018 2:25 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Wed Oct 17, 2007 9:29 am 
Offline
Forum Contributor
User avatar

Joined: Tue Sep 11, 2007 3:25 pm
Posts: 316
(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).

Syntax: [ Download ] [ Hide ]
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 17, 2007 10:31 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6445
Location: 53.01N x 112.48W
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 17, 2007 11:12 am 
Offline
Forum Contributor
User avatar

Joined: Tue Sep 11, 2007 3:25 pm
Posts: 316
Oops, I didn't know this forum existed either. :P


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 22, 2007 2:05 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
...but code must pass the critique first before it is allowed into Code Snippets.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 1:24 pm 
Offline
Moderator
User avatar

Joined: Sat Apr 20, 2002 3:14 pm
Posts: 1416
Location: Dallas, TX
Quick question, why did you use date("U") instead of time()?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 1:32 pm 
Offline
The Ninja Space Mod
User avatar

Joined: Fri Aug 05, 2005 1:53 pm
Posts: 6424
Location: Paradise, CA
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 1:40 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 2:43 pm 
Offline
Tranquility In Moderation
User avatar

Joined: Sun Feb 06, 2005 8:18 pm
Posts: 5001
Location: Indiana
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group