PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.1.7
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.1.7
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / includes / class-mainwp-datetime.php

class-mainwp-datetime.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.1.7, at includes/class-mainwp-datetime.php

108 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Wrapper for PHP DateTime which adds support for gmt/utc offset when a
4 * timezone is absent
5 *
6 * @since 5.1.1
7 * @package MainWP\Dashboard
8 * @author Woocommerce Authors.
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Datetime class.
15 */
16 class MainWP_DateTime extends DateTime {
17
18 /**
19 * UTC Offset, if needed. Only used when a timezone is not set. When
20 * timezones are used this will equal 0.
21 *
22 * @var integer
23 */
24 protected $utc_offset = 0;
25
26 /**
27 * Output an ISO 8601 date string in local (WordPress) timezone.
28 *
29 * @since 3.0.0
30 * @return string
31 */
32 public function __toString() {
33 return $this->format( DATE_ATOM );
34 }
35
36 /**
37 * Set UTC offset - this is a fixed offset instead of a timezone.
38 *
39 * @param int $offset Offset.
40 */
41 public function set_utc_offset( $offset ) {
42 $this->utc_offset = intval( $offset );
43 }
44
45 /**
46 * Get UTC offset if set, or default to the DateTime object's offset.
47 */
48 #[\ReturnTypeWillChange]
49 public function getOffset() {
50 return $this->utc_offset ? $this->utc_offset : parent::getOffset();
51 }
52
53 /**
54 * Set timezone.
55 *
56 * @param DateTimeZone $timezone DateTimeZone instance.
57 * @return DateTime
58 */
59 #[\ReturnTypeWillChange]
60 public function setTimezone( $timezone ) {
61 $this->utc_offset = 0;
62 return parent::setTimezone( $timezone );
63 }
64
65 /**
66 * Missing in PHP 5.2 so just here so it can be supported consistently.
67 *
68 * @since 3.0.0
69 * @return int
70 */
71 #[\ReturnTypeWillChange]
72 public function getTimestamp() {
73 return method_exists( 'DateTime', 'getTimestamp' ) ? parent::getTimestamp() : $this->format( 'U' );
74 }
75
76 /**
77 * Get the timestamp with the WordPress timezone offset added or subtracted.
78 *
79 * @since 3.0.0
80 * @return int
81 */
82 public function getOffsetTimestamp() {
83 return $this->getTimestamp() + $this->getOffset();
84 }
85
86 /**
87 * Format a date based on the offset timestamp.
88 *
89 * @since 3.0.0
90 * @param string $format Date format.
91 * @return string
92 */
93 public function date( $format ) {
94 return gmdate( $format, $this->getOffsetTimestamp() );
95 }
96
97 /**
98 * Return a localised date based on offset timestamp. Wrapper for date_i18n function.
99 *
100 * @since 3.0.0
101 * @param string $format Date format.
102 * @return string
103 */
104 public function date_i18n( $format = 'Y-m-d' ) {
105 return date_i18n( $format, $this->getOffsetTimestamp() );
106 }
107 }
108