Нет описания

ActionScheduler_DateTime.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * ActionScheduler DateTime class.
  4. *
  5. * This is a custom extension to DateTime that
  6. */
  7. class ActionScheduler_DateTime extends DateTime {
  8. /**
  9. * UTC offset.
  10. *
  11. * Only used when a timezone is not set. When a timezone string is
  12. * used, this will be set to 0.
  13. *
  14. * @var int
  15. */
  16. protected $utcOffset = 0;
  17. /**
  18. * Get the unix timestamp of the current object.
  19. *
  20. * Missing in PHP 5.2 so just here so it can be supported consistently.
  21. *
  22. * @return int
  23. */
  24. public function getTimestamp() {
  25. return method_exists( 'DateTime', 'getTimestamp' ) ? parent::getTimestamp() : $this->format( 'U' );
  26. }
  27. /**
  28. * Set the UTC offset.
  29. *
  30. * This represents a fixed offset instead of a timezone setting.
  31. *
  32. * @param $offset
  33. */
  34. public function setUtcOffset( $offset ) {
  35. $this->utcOffset = intval( $offset );
  36. }
  37. /**
  38. * Returns the timezone offset.
  39. *
  40. * @return int
  41. * @link http://php.net/manual/en/datetime.getoffset.php
  42. */
  43. public function getOffset() {
  44. return $this->utcOffset ? $this->utcOffset : parent::getOffset();
  45. }
  46. /**
  47. * Set the TimeZone associated with the DateTime
  48. *
  49. * @param DateTimeZone $timezone
  50. *
  51. * @return static
  52. * @link http://php.net/manual/en/datetime.settimezone.php
  53. */
  54. public function setTimezone( $timezone ) {
  55. $this->utcOffset = 0;
  56. parent::setTimezone( $timezone );
  57. return $this;
  58. }
  59. /**
  60. * Get the timestamp with the WordPress timezone offset added or subtracted.
  61. *
  62. * @since 3.0.0
  63. * @return int
  64. */
  65. public function getOffsetTimestamp() {
  66. return $this->getTimestamp() + $this->getOffset();
  67. }
  68. }