We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

mattkenefick on 05/20/08


Tagged


Versions (?)


PHP: Timezone Adjustment


Published in: PHP 


Converts current time to that of another timezone. Custom Variables: $user->personal_data['timezone'] // users' timezone

  1. # Timezone Adjustments
  2. function timezone_get_gmz( $input ){
  3. if( !isset($input) )
  4. $input = time();
  5. // return offset of unix time - offset of time
  6. // date O returns system's timezone offset: -0500.. -0800..
  7. // therefore, hours turn from 3600 to 36
  8. return $input-(date('O')*36);
  9. }
  10. function timezone_convert($time, $hoursOffset = -5){
  11. // default of EST
  12. // - hoursOffset because offset for EST is 5.. but you subtract it
  13. // this will multiply GMZ time by the hours offset defined
  14. return timezone_get_gmz($time)+(3600*(-$hoursOffset));
  15. }
  16. function timezone_makeDate ($format='',$input){
  17. global $user;
  18.  
  19. // check for input :: Default = time ()
  20. // convert input to unix if not a number
  21. // get user timezone if it's set
  22. // apply timezone time to time and return in format of date
  23.  
  24. if( !isset( $input ) )
  25. $input = time();
  26. if( !is_numeric( $input ) )
  27. $input = strtotime($input);
  28. if( isset($user->personal_data['timezone']) )
  29. $timezone = $user->personal_data['timezone'];
  30. else
  31. $timezone = 5;
  32. if ($format=='')
  33. return timezone_convert($input,$timezone);
  34. else
  35. return date($format,timezone_convert($input,$timezone) );
  36. }

Report this snippet 

You need to login to post a comment.