RobertP Posted June 12, 2012 Share Posted June 12, 2012 has anyone attempted this? php version:5.4.3 function getTimeByZone($zone){ $default = ini_get('date.timezone'); ini_set('date.timezone',$zone); $time = time(); ini_set('date.timezone',$default); return $time; } echo getTimeByZone('America/Toronto').'<br />'; echo getTimeByZone('Europe/Amsterdam').'<br />'; echo getTimeByZone('GMT-8').'<br />'; outputs all the same. ps: i know i should use http://php.net/manual/en/book.datetime.php, but i cant seem to get it working properly, same output as above. $date->setTimezone(new DateTimeZone('GMT-8') ); Quote Link to comment https://forums.phpfreaks.com/topic/264030-current-time-in-different-timezones/ Share on other sites More sharing options...
requinix Posted June 12, 2012 Share Posted June 12, 2012 time() is the number of seconds since the beginning of 1970 local time. As such it is not affected by timezones. What is affected is what you get when you convert that number into a date string. Also, use date_default_timezone_get/set() if you go procedural. function getTimeByZone($zone){ $old = date_default_timezone_get(); date_default_timezone_set($zone); $time = date('r'); date_default_timezone_set($old); return $time; } Quote Link to comment https://forums.phpfreaks.com/topic/264030-current-time-in-different-timezones/#findComment-1353073 Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2012 Share Posted June 12, 2012 Edit: basically says the same as what requinix posted above ^^^ The time function returns the current Unix timestamp. Time uses the computer's/server's clock and timezone setting (if you change your computer's clock or timezone setting so that they are it is wrong for your location, you will get the timestamp based on wherever that wrong time/timezone corresponds to.) Edit2: or I could just be wrong about everything I wrote here concerning changing the timezone giving a different timestamp. Only functions like date, strtotime, mktime, and a few others, actually use php's timezone setting. Quote Link to comment https://forums.phpfreaks.com/topic/264030-current-time-in-different-timezones/#findComment-1353074 Share on other sites More sharing options...
RobertP Posted June 12, 2012 Author Share Posted June 12, 2012 would this work? function getTimeByZone($zone){ return strtotime((new DateTime(null,new DateTimeZone($zone)))->format('Y-m-d H:i:sP')); } ps: i need the current unix timestamp for the zone Quote Link to comment https://forums.phpfreaks.com/topic/264030-current-time-in-different-timezones/#findComment-1353151 Share on other sites More sharing options...
RobertP Posted June 12, 2012 Author Share Posted June 12, 2012 i think i got it working? can someone test aswell please? function getTimeByZone($zone){ $default = date_default_timezone_get(); date_default_timezone_set($zone); $time = time()+date('Z'); date_default_timezone_set($default); return $time; } Quote Link to comment https://forums.phpfreaks.com/topic/264030-current-time-in-different-timezones/#findComment-1353193 Share on other sites More sharing options...
requinix Posted June 12, 2012 Share Posted June 12, 2012 Like I said, timestamps have nothing to do with timezones. It's the same number everywhere. Like right now it's 1339523573 (give or take) regardless of where you are. That corresponds to 10:52am PDT and 1:52pm EDT. Let me be even more explicit: printf("Timezone: %s\n", date_default_timezone_get()); printf("Unix timestamp: %u\n", time()); printf("Time: %s\n", date("Y-m-d H:i:s")); printf("\n"); date_default_timezone_set("UTC"); printf("Timezone: %s\n", date_default_timezone_get()); printf("Unix timestamp: %u\n", time()); printf("Time: %s\n", date("Y-m-d H:i:s")); printf("\n"); date_default_timezone_set("Asia/Shanghai"); printf("Timezone: %s\n", date_default_timezone_get()); printf("Unix timestamp: %u\n", time()); printf("Time: %s\n", date("Y-m-d H:i:s")); Timezone: America/Los_Angeles Unix timestamp: 1339523573 Time: 2012-06-12 10:52:53 Timezone: UTC Unix timestamp: 1339523573 Time: 2012-06-12 17:52:53 Timezone: Asia/Shanghai Unix timestamp: 1339523573 Time: 2012-06-13 01:52:53 Your problem is you're using the wrong timezone for something. You should never adjust a time() value to use a different timezone: use date_default_timezone_set() to change the timezone then date() to get a date string. (Or the OOP equivalent.) Quote Link to comment https://forums.phpfreaks.com/topic/264030-current-time-in-different-timezones/#findComment-1353232 Share on other sites More sharing options...
RobertP Posted June 18, 2012 Author Share Posted June 18, 2012 function getTimeByZone($zone){ $default = date_default_timezone_get(); $defaultOffset = date('Z'); date_default_timezone_set($zone); $offset = date('Z'); date_default_timezone_set($default); return ($offset-$defaultOffset)+time(); } echo date('d-m-Y h:i:s a',getTimeByZone('America/Toronto')).'<br />'; echo date('d-m-Y h:i:s a',getTimeByZone('Europe/Lisbon')).'<br />'; echo date('d-m-Y h:i:s a',getTimeByZone('Asia/Tokyo')).'<br />'; echo date('d-m-Y h:i:s a',getTimeByZone('Pacific/Samoa')).'<br />'; exit; results: 17-06-2012 09:15:24 pm 18-06-2012 02:15:24 am 18-06-2012 10:15:24 am 17-06-2012 02:15:24 pm Quote Link to comment https://forums.phpfreaks.com/topic/264030-current-time-in-different-timezones/#findComment-1354742 Share on other sites More sharing options...
requinix Posted June 19, 2012 Share Posted June 19, 2012 No. Are you not paying attention? Sticking with your code and just fiddling with it until it works? function dateByZone($format, $zone){ $default = date_default_timezone_get(); date_default_timezone_set($zone); $string = date($format); date_default_timezone_set($default); return $string; } echo dateByZone('d-m-Y h:i:s a', 'America/Toronto') . ' '; Quote Link to comment https://forums.phpfreaks.com/topic/264030-current-time-in-different-timezones/#findComment-1355041 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.