date - PHP timestamps & timezone configuration -


my plan:

  • get current timestamp using strtotime("now")
  • convert timezone '0' - part don't know how do. have number represents users timezone, -8 hours example.
  • store in database in timezone '0'
  • retrieve database in timezone '0'
  • convert users timezone in opposite direction
  • use date('', timestamp) function display it

how can accomplish conversion? or going wrong?

i need able store in database numerically represented time (like strtotime returns)

using time() same strtotime("now") , not need worry converting timezone of timestamp, timestamp has no timezone:

does php time() return gmt/utc timestamp?

time returns unix timestamp, timezone independent. since unix timestamp denotes seconds since 1970 utc it's utc, has no timezone.

you can store timestamp in database. when retrieve can convert users timezone. this:

date_default_timezone_set('utc');  $timestamp = '1429066967'; //supported timezones: http://php.net/manual/en/timezones.php $usertimezone = 'america/los_angeles';  $dt = new datetime(); // set timestamp $dt->settimestamp($timestamp); // set timezone $dt->settimezone(new datetimezone($usertimezone)); // format date $date = $dt->format('y-m-d h:i:s');  echo $date; 

outputs: 2015-04-14 20:02:47

but if have utc offset try this:

date_default_timezone_set('utc');  $timestamp = '1429066967'; $offset = -8; $usertimezone = timezone_name_from_abbr("", $offset*3600, false);   $dt = new datetime(); // set timestamp $dt->settimestamp($timestamp); // set timezone $dt->settimezone(new datetimezone($usertimezone)); // format date $date = $dt->format('y-m-d h:i:s');  echo $date; 

which outputs: 2015-04-14 20:02:47


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -