INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

E-mail*
Handle

Password
Verify P'word
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Member Feedback

"...You have made an incredible site which is truly a great help to me in solving problems. A tip of my hat to you!..."

Geography

Where in the world do Tek-Tips members come from?

 PHP - Convert server timestamp to localtime?

cmayo (MIS)
23 Jan 08 18:47
Sorry if this is been asked before, but I need to convert a server timestamp in the form of "YYYY-MM-DD HH:MM:SS" to a different timezone.

I have the local GMT offset in a configuration variable, is there an easy way to maybe convert the server timestamp to GMT, then apply the local offset to the GMT timestamp and arrive at a localized version of the timestamp? How would I code that?

Or am I making this too hard?

Thanks,
Chuck
Check Out Our Whitepaper Library. Click Here.
jpadie (TechnicalUser)
24 Jan 08 2:54
let's assume that your server is based in New York (EST) and you want to convert to your local timezone, based in Toulouse, France (CET).

CODE

//set local timezone
date_default_timezone_set("Europe/Paris");

//get server time as a unix timestamp
$unixtime = strtotime($servertime . ' EST'); //append EST to denote New York time

//convert to human readable local time
echo 'server time is '.date('r', $unixtime);
cmayo (MIS)
24 Jan 08 11:11
Thanks very much for the suggestion, but my server's running PHP 4.4.4 and doesn't support date_default_timezone_set.

Also, I really need something that'll convert a MySQL-type timestamp like "2008-01-24 09:57:17" (server time) to the same sort of timestamp adjusted to the users' local time, i.e. "2008-01-24 07:57:17."

I did find some code online which seems to nicely convert the current server time to local time...

CODE

// Convert current servertime to GMT so the local time can be calculated
function fetchGMT()
{
    return mktime () + ((date('O')/100 )/-1)*60*60 ;
}
 
// What is the hour offset for OUR timezone?
// We are in GMT -6, so -6 is our offset!
$localoffset=-6 ;

$gmt=fetchGMT();  // Get GMT time
$timezoneoffset=$localoffset* 60*60; // Get offset between local and GMT
$local_time=$gmt+$timezoneoffset ; // Add offset to get local time  

...but I'm getting myself all tripped up trying to modify that function to accept and return the timestamp. I dunno why these time functions confuse me so much...

Any ideas?
jpadie (TechnicalUser)
24 Jan 08 11:51

CODE

//set local timezone
putenv("TZ=Europe/Paris");

//get server time as a unix timestamp
$unixtime = strtotime($servertime . ' EST'); //append EST to denote New York time

//convert to human readable local time
echo 'server time is '.date('r', $unixtime);

your code is just a maths solution - try to use the built in functions of php to handle the time zone (and daylight savings time etc) conversions for you.

Start A New Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Promoting, selling, recruiting and student posting
are not allowed in the forums.
Posting Policies

LINK TO THIS FORUM!
(Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum)
TITLE: PHP Forum at Tek-Tips
URL: http://www.tek-tips.com/threadminder.cfm?pid=434
DESCRIPTION: PHP technical support forum and mutual help system for computer professionals. Selling and recruiting forbidden.

 

Back To Forum