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.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / helpers / DateTimeHelper.php
tutor / helpers Last commit date
ComponentHelper.php 16 hours ago DateTimeHelper.php 16 hours ago HttpHelper.php 11 months ago PluginInstaller.php 1 year ago QueryHelper.php 16 hours ago SessionHelper.php 3 years ago TimerHelper.php 16 hours ago UrlHelper.php 16 hours ago ValidationHelper.php 10 months ago
DateTimeHelper.php
306 lines
1 <?php
2 /**
3 * Helper class to work with datetime.
4 *
5 * @package Tutor\Helper
6 * @author Themeum <support@themeum.com>
7 * @link https://themeum.com
8 * @since 3.0.0
9 */
10
11 namespace Tutor\Helpers;
12
13 use DateInterval;
14 use DateTime;
15 use DateTimeZone;
16
17 /**
18 * DateTimeHelper class
19 *
20 * @since 3.0.0
21 */
22 final class DateTimeHelper {
23 /**
24 * Format constants
25 */
26 const FORMAT_MYSQL = 'Y-m-d H:i:s';
27 const FORMAT_DATE_TIME = 'Y-m-d H:i:s';
28 const FORMAT_DATE = 'Y-m-d';
29 const FORMAT_TIME = 'H:i:s';
30 const FORMAT_TIMESTAMP = 'U';
31
32 /**
33 * Interval constants
34 */
35 const INTERVAL_HOUR = 'hour';
36 const INTERVAL_DAY = 'day';
37 const INTERVAL_WEEK = 'week';
38 const INTERVAL_MONTH = 'month';
39 const INTERVAL_YEAR = 'year';
40
41 /**
42 * Date
43 *
44 * @var DateTime
45 */
46 private $datetime;
47
48 /**
49 * Create an instance.
50 */
51 private static function instance() {
52 return new self();
53 }
54
55 /**
56 * Get current time.
57 *
58 * @return self
59 */
60 public static function now() {
61 $instance = self::instance();
62 $instance->datetime = new DateTime();
63 return $instance;
64 }
65
66 /**
67 * Create time from date time string or timestamp.
68 *
69 * @param string|int $datetime datetime string or timestamp.
70 * @param string|DateTimeZone $timezone timezone string or object.
71 *
72 * @return self
73 */
74 public static function create( $datetime, $timezone = null ) {
75 $instance = self::instance();
76 $instance->datetime = new DateTime(
77 $datetime,
78 $timezone instanceof DateTimeZone ? $timezone : ( $timezone ? new DateTimeZone( $timezone ) : null )
79 );
80
81 return $instance;
82 }
83
84 /**
85 * Set timezone
86 *
87 * @param string|DateTimeZone $timezone timezone string or object.
88 *
89 * @return self
90 */
91 public function set_timezone( $timezone ) {
92 $tz = is_string( $timezone ) ? new DateTimeZone( $timezone ) : $timezone;
93 $this->datetime->setTimezone( $tz );
94 return $this;
95 }
96
97 /**
98 * Add
99 *
100 * @param int $number number of interval.
101 * @param string $interval interval type (day, month, year, etc).
102 *
103 * @return self
104 */
105 public function add( $number, $interval ) {
106 $this->datetime->add( DateInterval::createFromDateString( "{$number} {$interval}" ) );
107 return $this;
108 }
109
110 /**
111 * Sub
112 *
113 * @param int $number number of interval.
114 * @param string $interval interval type (day, month, year, etc).
115 *
116 * @since 3.0.0
117 *
118 * @return self
119 */
120 public function sub( $number, $interval ) {
121 $this->datetime->sub( DateInterval::createFromDateString( "{$number} {$interval}" ) );
122 return $this;
123 }
124
125 /**
126 * Get DateTime obj.
127 *
128 * @since 4.0.0
129 *
130 * @return DateTime
131 */
132 public function get() {
133 return $this->datetime;
134 }
135
136 /**
137 * Check date time is past
138 *
139 * @return boolean
140 */
141 public function is_past() {
142 return $this->datetime->getTimestamp() < time();
143 }
144
145 /**
146 * Check date time is future
147 *
148 * @return boolean
149 */
150 public function is_future() {
151 return $this->datetime->getTimestamp() > time();
152 }
153
154 /**
155 * Get timezone
156 *
157 * @return DateTimeZone
158 */
159 public function get_timezone() {
160 return $this->datetime->getTimezone();
161 }
162
163 /**
164 * Get timezone string
165 *
166 * @return string
167 */
168 public function get_timezone_string() {
169 return $this->datetime->getTimezone()->getName();
170 }
171
172 /**
173 * Format datetime ( WP i18 translation supported )
174 *
175 * @param string $format format for date. Default is mysql format.
176 * @param bool $i18_translation i18 translation support.
177 *
178 * @return string
179 */
180 public function format( $format = null, $i18_translation = true ) {
181 if ( $i18_translation ) {
182 $result = wp_date(
183 $format ? $format : self::FORMAT_MYSQL,
184 $this->to_timestamp(),
185 $this->datetime->getTimezone()
186 );
187 } else {
188 $result = $this->datetime->format( $format ? $format : self::FORMAT_MYSQL );
189 }
190
191 return $result;
192 }
193
194 /**
195 * Get readable time difference.
196 *
197 * @return string
198 */
199 public function get_readable_diff() {
200 $now = new DateTime( 'now', $this->datetime->getTimezone() );
201 // Calculate the time difference in seconds.
202 $interval = $now->getTimestamp() - $this->datetime->getTimestamp();
203
204 // Use WordPress's human_time_diff for minutes and beyond.
205 $time_diff = human_time_diff( $this->datetime->getTimestamp(), $now->getTimestamp() );
206
207 // Handle past or future tense.
208 return ( $interval > 0 )
209 /* translators: %s human-readable time difference. */
210 ? sprintf( __( '%s ago', 'tutor' ), $time_diff )
211 /* translators: %s: time difference */
212 : sprintf( __( '%s from now', 'tutor' ), $time_diff );
213 }
214
215 /**
216 * Convert to timestamp.
217 *
218 * @return int
219 */
220 public function to_timestamp() {
221 return $this->datetime->getTimestamp();
222 }
223
224 /**
225 * Covert to date time string.
226 *
227 * @return string
228 */
229 public function to_date_time_string() {
230 return $this->format( self::FORMAT_DATE_TIME, false );
231 }
232
233 /**
234 * Covert to date string.
235 *
236 * @return string
237 */
238 public function to_date_string() {
239 return $this->format( self::FORMAT_DATE, false );
240 }
241
242 /**
243 * Covert to date string.
244 *
245 * @return string
246 */
247 public function to_time_string() {
248 return $this->format( self::FORMAT_TIME, false );
249 }
250
251 /**
252 * Get GMT date to user timezone date.
253 *
254 * @since 3.0.0
255 *
256 * @param string $gmt_date gmt date time string.
257 * @param string $format format string.
258 * @param int|object $user id or object. 0 for current user (optional).
259 *
260 * @return string
261 */
262 public static function get_gmt_to_user_timezone_date( string $gmt_date, ?string $format = null, $user = 0 ): string {
263 $default_format = get_option( 'date_format' ) . ', ' . get_option( 'time_format' );
264 $format = is_null( $format ) ? $default_format : $format;
265
266 return self::create( $gmt_date )
267 ->set_timezone( \TUTOR\User::get_user_timezone_string( $user ) )
268 ->format( $format );
269 }
270
271 /**
272 * Convert user timezone date time to GMT date time
273 *
274 * @since 3.0.0
275 *
276 * @param string $datetime date time string.
277 * @param string|null $timezone user' timezone string, default is WordPress timezone.
278 *
279 * @return string GMT date time in MySQL format
280 */
281 public static function get_user_time_to_gmt_time( string $datetime, $timezone = null ): string {
282 $timezone = $timezone ? $timezone : wp_timezone_string();
283
284 return self::create( $datetime, $timezone )
285 ->set_timezone( 'UTC' )
286 ->format( self::FORMAT_MYSQL );
287 }
288
289 /**
290 * Format seconds into an associative array of hours, minutes, and seconds.
291 *
292 * @since 4.0.0
293 *
294 * @param int $seconds Total seconds.
295 *
296 * @return array{hours: int, minutes: int, seconds: int}
297 */
298 public static function split_seconds_into_time_units( int $seconds ): array {
299 return array(
300 'hours' => intdiv( $seconds, HOUR_IN_SECONDS ),
301 'minutes' => intdiv( $seconds % HOUR_IN_SECONDS, MINUTE_IN_SECONDS ),
302 'seconds' => $seconds % MINUTE_IN_SECONDS,
303 );
304 }
305 }
306