Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. date('Y-m-d H:i:s', strtotime($Site->getUTCOffset() . ' hours', strtotime(date($utcDate))));
  2.  
  3. <?php
  4. /*
  5. Example usage:
  6. $unixtime = TimeUtil::dateTimeToTimestamp('2009-04-01 15:36:13');
  7. echo TimeUtil::UTCToPST("M d, Y - H:i:s", $unixtime);
  8. */
  9.  
  10. // You should move this to your regular init method
  11. date_default_timezone_set('UTC'); // make this match the server timezone
  12.  
  13. class TimeUtil {
  14. public static function timestampToDateTime($timestamp) {
  15. return gmdate('Y-m-d H:i:s', $timestamp);
  16. }
  17.  
  18. public static function dateTimeToTimestamp($dateTime) {
  19. // dateTimeToTimestamp expects MySQL format
  20. // If it gets a fully numeric value, we'll assume it's a timestamp
  21. // You can comment out this if block if you don't want this behavior
  22. if(is_numeric($dateTime)) {
  23. // You should probably log an error here
  24. return $dateTime;
  25. }
  26. $date = new DateTime($dateTime);
  27. $ret = $date->format('U');
  28. return ($ret < 0 ? 0 : $ret);
  29. }
  30.  
  31. public static function UTCToPST($format, $time) {
  32. $dst = intval(date("I", $time));
  33. $tzOffset = intval(date('Z', time()));
  34. return date($format, $time + $tzOffset - 28800 + $dst * 3600);
  35. }
  36.  
  37. }
  38.  
  39. <?php
  40.  
  41. $mysqlDate = '2009-04-01 15:36:13';
  42.  
  43. $dateTime = new DateTime ($mysqlDate);
  44. $dateTime->setTimezone(new DateTimeZone('America/Los_Angeles'));
  45.  
  46. ?>
  47.  
  48. function convert_time_zone($date_time, $from_tz, $to_tz)
  49. {
  50. $time_object = new DateTime($date_time, new DateTimeZone($from_tz));
  51. $time_object->setTimezone(new DateTimeZone($to_tz));
  52. return $time_object->format('Y-m-d H:i:s');
  53. }
  54.  
  55. mysql> set time_zone='America/New_York';
  56.  
  57. ALTER TABLE mytable CHANGE COLUMN Created Created timestamp NULL DEFAULT 0;
  58.  
  59. <?php
  60.  
  61. function getNoteDateTimeZone($date = null, $from_dtz = 'US/Central', $to_dtz = null) {
  62. //$from_zt = 'US/Central'; // Time Zone = -06:00
  63. if (is_null($date) == FALSE && is_null($from_dtz) == FALSE && is_null($to_dtz) == FALSE) {
  64. // set TimeZone from
  65. $time_object = new DateTime($date, new DateTimeZone($from_dtz));
  66. $time_now_object = new DateTime("now", new DateTimeZone($from_dtz));
  67. // Change TimeZone
  68. $time_object->setTimezone(new DateTimeZone(trim($to_dtz)));
  69. $time_now_object->setTimezone(new DateTimeZone(trim($to_dtz)));
  70. // Is day = day in $time_now_object, $time_object..?
  71. if ($time_now_object->format('d') == $time_object->format('d')) {
  72. return $time_object->format('H:i:s');
  73. } else {
  74. return $time_object->format('Y-m-d H:i:s');
  75. }
  76. } else {
  77. return '';
  78. }
  79. }
  80. ?>
  81.  
  82. <?php
  83. $date = '2008-06-02 20:32:46';
  84. $dtz = 'America/Argentina/Buenos_Aires';
  85. echo getNoteDateTimeZone($date, 'US/Central', $dtz); // Out = 2008-06-02 23:32:46
  86. ?>
  87.  
  88. ADDTIME($utcDate,$Site->getUTCOffset())
  89.  
  90. function convert_to_user_date($date, $userTimeZone = 'America/Los_Angeles', $serverTimeZone = 'UTC', $format = 'n/j/Y g:i A')
  91. {
  92. $dateTime = new DateTime ($date, new DateTimeZone($serverTimeZone));
  93. $dateTime->setTimezone(new DateTimeZone($userTimeZone));
  94. return $dateTime->format($format);
  95. }
  96.  
  97. function convert_to_server_date($date, $userTimeZone = 'America/Los_Angeles', $serverTimeZone = 'UTC', $format = 'n/j/Y g:i A')
  98. {
  99. $dateTime = new DateTime ($date, new DateTimeZone($userTimeZone));
  100. $dateTime->setTimezone(new DateTimeZone($serverTimeZone));
  101. return $dateTime->format($format);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement