You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.1 KiB

  1. <?php
  2. namespace p3k\date;
  3. use DateTime, DateTimeZone;
  4. // $format - one of the php.net/date format strings
  5. // $date - a string that will be passed to DateTime()
  6. // $offset - integer timezone offset in seconds
  7. function format_local($format, $date, $offset) {
  8. if($offset != 0)
  9. $tz = new DateTimeZone(($offset < 0 ? '-' : '+') . sprintf('%02d:%02d', abs(floor($offset / 60 / 60)), (($offset / 60) % 60)));
  10. else
  11. $tz = new DateTimeZone('UTC');
  12. $d = new DateTime($date);
  13. $d->setTimeZone($tz);
  14. return $d->format($format);
  15. }
  16. function tz_offset_to_seconds($offset) {
  17. if(preg_match('/([+-])(\d{2}):?(\d{2})/', $offset, $match)) {
  18. $sign = ($match[1] == '-' ? -1 : 1);
  19. return (($match[2] * 60 * 60) + ($match[3] * 60)) * $sign;
  20. } else {
  21. return 0;
  22. }
  23. }
  24. function tz_seconds_to_offset($seconds) {
  25. return ($seconds < 0 ? '-' : '+') . sprintf('%02d:%02d', abs($seconds/60/60), ($seconds/60)%60);
  26. }
  27. function tz_seconds_to_timezone($seconds) {
  28. if($seconds != 0)
  29. $tz = new DateTimeZone(tz_seconds_to_offset($seconds));
  30. else
  31. $tz = new DateTimeZone('UTC');
  32. return $tz;
  33. }