PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.4
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.4
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / class-lp-datetime.php

class-lp-datetime.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.4, at inc/class-lp-datetime.php

397 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class LP_Datetime
5 */
6 class LP_Datetime {
7 /**
8 * @var string $format .
9 */
10 public static $format = 'Y-m-d H:i:s';
11 /**
12 * Format date by config WP.
13 */
14 const I18N_FORMAT = 'i18n';
15 /**
16 * Format date time by config WP.
17 */
18 const I18N_FORMAT_HAS_TIME = 'i18n_has_time';
19 /**
20 * Format date time Human.
21 */
22 const HUMAN_FORMAT = 'human';
23 /**
24 * String date time.
25 *
26 * @var string $raw_date .
27 */
28 protected $raw_date = null;
29
30 /**
31 * Constructor.
32 *
33 * @param string|int $date
34 * @param mixed $tz
35 *
36 * @throws
37 */
38 public function __construct( $date = '', $tz = null ) {
39 if ( $date instanceof LP_Datetime ) {
40 $this->raw_date = $date->get_raw_date();
41 } else {
42 $this->raw_date = is_numeric( $date ) ? gmdate( self::$format, $date ) : $date;
43 }
44
45 if ( empty( $this->raw_date ) ) {
46 $this->raw_date = current_time( 'mysql', 1 );
47 }
48 }
49
50 /**
51 * Check date is null
52 *
53 * @return bool
54 */
55 public function is_null(): bool {
56 return ! $this->raw_date || $this->raw_date === '0000-00-00 00:00:00';
57 }
58
59 public function get_raw_date() {
60 return $this->raw_date;
61 }
62
63 /**
64 * @return string The date as a formatted string.
65 */
66 public function __toString() {
67 return $this->format( self::$format );
68 }
69
70 /**
71 * Gets the date as a formatted string.
72 *
73 * @param string $format Set i18n to return date in local time.
74 *
75 * @return string.
76 * @version 4.0.2
77 * @since 3.0.0
78 */
79 public function format( string $format = '' ): string {
80 $date_str = '';
81
82 if ( '0000-00-00 00:00:00' === $this->get_raw_date() ) {
83 return $date_str;
84 }
85
86 if ( empty( $format ) ) {
87 $format = get_option( 'date_format', 'Y-m-d' );
88 }
89
90 switch ( $format ) {
91 case 'i18n': // Display format Date by Timezone of WP.
92 $time_stamp = $this->getTimestamp(); // UTC+0 (GMT)
93 $time_stamp_by_time_zone = $time_stamp + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
94 $date_format = get_option( 'date_format' );
95 $date_str = date_i18n( $date_format, $time_stamp_by_time_zone );
96 break;
97 case 'i18n_has_time': // Display format Date Time by Timezone of WP.
98 $date_time_format_wp = apply_filters(
99 'learn-press/datetime/format/i18n_has_time',
100 get_option( 'date_format' ) . ' ' . get_option( 'time_format' )
101 );
102 $time_stamp = $this->getTimestamp(); // UTC+0 (GMT)
103 $time_stamp_by_time_zone = $time_stamp + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
104 $date_str = apply_filters(
105 'learn-press/datetime/date/i18n_has_time',
106 sprintf(
107 '%s',
108 date_i18n( $date_time_format_wp, $time_stamp_by_time_zone )
109 ),
110 $time_stamp,
111 $time_stamp_by_time_zone
112 );
113 break;
114 case 'human':
115 $time = $this->getTimestamp();
116 $time_diff = time() - $time;
117
118 if ( $time_diff > 0 ) {
119 $date_str = sprintf( __( '%s ago', 'learnpress' ), human_time_diff( $time, time() ) );
120 }
121 break;
122 case 'mysql':
123 $date_str = gmdate( 'Y-m-d H:i:s', $this->getTimestamp() );
124 break;
125 default:
126 $date_str = gmdate( $format, $this->getTimestamp() );
127 }
128
129 if ( ! is_string( $date_str ) ) {
130 $date_str = '';
131 }
132
133 return $date_str;
134 }
135
136 /**
137 * Display date human time diff.
138 * 1. Show number days, hours if >= 1 days
139 * 2. Show number hours, seconds if >= 1 hours
140 * 3. Show number seconds if < 1 hours
141 *
142 * @param DateTime $date_start
143 * @param DateTime $date_end
144 *
145 * @return string
146 * @since 4.0.3
147 * @version 1.0.5
148 */
149 public static function format_human_time_diff( DateTime $date_start, DateTime $date_end ): string {
150 $diff = $date_end->diff( $date_start );
151 $week = floor( $diff->d / 7 );
152
153 $i18n_year = self::get_string_plural_duration( $diff->y, 'year' );
154 $i18n_month = self::get_string_plural_duration( $diff->m, 'month' );
155 $i18n_week = self::get_string_plural_duration( $week, 'week' );
156 $i18n_day = self::get_string_plural_duration( $diff->d, 'day' );
157 $i18n_hour = self::get_string_plural_duration( $diff->h, 'hour' );
158 $i18n_minute = self::get_string_plural_duration( $diff->i, 'minute' );
159 $i18n_second = self::get_string_plural_duration( $diff->s, 'second' );
160
161 $format_date = '';
162 $string = array(
163 'y' => '%y years',
164 'm' => '%m months',
165 'w' => '', // object don't have week, only add to custom week format
166 'd' => '%d days, %h hours',
167 'h' => '%h hours, %i minutes',
168 'i' => '%i minutes, %s seconds',
169 's' => $i18n_second,
170 );
171
172 foreach ( $string as $k => $v ) {
173 if ( isset( $diff->{$k} ) && $diff->{$k} > 0 ) {
174 switch ( $k ) {
175 case 'y':
176 $format_date = sprintf(
177 '%1$s%2$s',
178 $i18n_year,
179 $diff->m > 0 ? ', ' . $i18n_month : ''
180 );
181 break;
182 case 'm':
183 $format_date = sprintf(
184 '%1$s%2$s',
185 $i18n_month,
186 $diff->d > 0 ? ', ' . $i18n_day : ''
187 );
188 break;
189 case 'd':
190 $format_date = sprintf(
191 '%1$s%2$s',
192 $i18n_day,
193 $diff->h > 0 ? ', ' . $i18n_hour : ''
194 );
195 break;
196 case 'h':
197 $format_date = sprintf(
198 '%1$s%2$s',
199 $i18n_hour,
200 $diff->i > 0 ? ', ' . $i18n_minute : ''
201 );
202 break;
203 case 'i':
204 $format_date = sprintf(
205 '%1$s%2$s',
206 $i18n_minute,
207 $diff->s > 0 ? ', ' . $i18n_second : ''
208 );
209 break;
210 default:
211 $format_date = $v;
212 break;
213 }
214 break;
215 } elseif ( 'w' === $k && $week > 0 ) {
216 $day_remain = $diff->d - $week * 7;
217 $format_date = sprintf(
218 '%1$s%2$s',
219 $i18n_week,
220 $day_remain > 0 ? ', ' . self::get_string_plural_duration( $day_remain, 'day' ) : ''
221 );
222 break;
223 }
224 }
225
226 return apply_filters(
227 'learn-press/datetime/format_human_time_diff',
228 $format_date,
229 $diff,
230 $date_start,
231 $date_end
232 );
233 }
234
235 /**
236 * Get the date as an SQL datetime string.
237 *
238 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
239 *
240 * @return string
241 * @version 4.0.1
242 * @since 3.0.0
243 */
244 public function toSql( bool $local = false ): string {
245 if ( $local ) {
246 return $this->toSqlLocal();
247 }
248
249 return $this->format( 'mysql' );
250 }
251
252 /**
253 * Convert to format sql local time.
254 *
255 * @return string
256 * @since 4.2.1
257 */
258 private function toSqlLocal(): string {
259 $time = $this->getTimestamp() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
260
261 return gmdate( LP_Datetime::$format, $time );
262 }
263
264 /**
265 * Convert local time to gmt+0 time.
266 *
267 * @param LP_Datetime $date_time_local
268 * @param string $format
269 *
270 * @return string
271 * @since 4.3.6
272 * @version 1.0.0
273 */
274 public function to_gmt_string( LP_Datetime $date_time_local, string $format = 'Y-m-d H:i:s' ): string {
275 $time_stamp = $date_time_local->getTimestamp() - $this->get_wp_option_gmt_offset() * HOUR_IN_SECONDS;
276 $time = new LP_Datetime( $time_stamp );
277 return $time->format( $format );
278 }
279
280 /**
281 * Get gmt offset from wp option.
282 *
283 * @return false|mixed|null
284 */
285 public function get_wp_option_gmt_offset() {
286 return get_option( 'gmt_offset' );
287 }
288
289 /**
290 * Get timestamp of Date.
291 *
292 * @return int
293 */
294 public function getTimestamp(): int {
295 try {
296 $date = new DateTime( $this->get_raw_date() );
297 } catch ( Throwable $e ) {
298 error_log( __METHOD__ . ': ' . $e->getMessage() );
299 $date = new DateTime();
300 }
301
302 return $date->getTimestamp();
303 }
304
305 /**
306 * Get timestamp of Date in local time.
307 * Note: timestamp before handle must timezone is GMT+0
308 *
309 * @return int
310 * @since 4.2.7.3
311 */
312 public function getTimestampLocal(): int {
313 return $this->getTimestamp() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
314 }
315
316 /**
317 * Get string plural duration.
318 *
319 * @param int $duration_number
320 * @param string $duration_type
321 *
322 * @return string
323 * @version 1.0.5
324 * @since 4.2.3.5
325 */
326 public static function get_string_plural_duration( int $duration_number, string $duration_type = '' ): string {
327 switch ( strtolower( $duration_type ) ) {
328 case 'second':
329 $duration_str = sprintf(
330 _n( '%s Second', '%s Seconds', $duration_number, 'learnpress' ),
331 $duration_number
332 );
333 break;
334 case 'minute':
335 $duration_str = sprintf(
336 _n( '%s Minute', '%s Minutes', $duration_number, 'learnpress' ),
337 $duration_number
338 );
339 break;
340 case 'hour':
341 $duration_str = sprintf(
342 _n( '%s Hour', '%s Hours', $duration_number, 'learnpress' ),
343 $duration_number
344 );
345 break;
346 case 'day':
347 $duration_str = sprintf(
348 _n( '%s Day', '%s Days', $duration_number, 'learnpress' ),
349 $duration_number
350 );
351 break;
352 case 'week':
353 $duration_str = sprintf(
354 _n( '%s Week', '%s Weeks', $duration_number, 'learnpress' ),
355 $duration_number
356 );
357 break;
358 case 'month':
359 $duration_str = sprintf(
360 _n( '%s Month', '%s Months', $duration_number, 'learnpress' ),
361 $duration_number
362 );
363 break;
364 case 'year':
365 $duration_str = sprintf(
366 _n( '%s Year', '%s Years', $duration_number, 'learnpress' ),
367 $duration_number
368 );
369 break;
370 default:
371 $duration_str = $duration_number . ' ' . $duration_type;
372 }
373
374 return apply_filters( 'learn-press/i18n/plural_duration', $duration_str, $duration_number, $duration_type );
375 }
376
377 /**
378 * Get timezone string
379 *
380 * @return string
381 * @since 4.2.7.2
382 * @version 1.0.0
383 */
384 public static function get_timezone_string(): string {
385 $wp_timezone = wp_timezone_string();
386 $is_utc = (int) $wp_timezone !== 0;
387
388 if ( $is_utc ) {
389 $wp_timezone = sprintf( '%s %s', __( 'Timezone: UTC', 'learnpress' ), $wp_timezone );
390 } else {
391 $wp_timezone = sprintf( '%s %s', __( 'Timezone:', 'learnpress' ), $wp_timezone );
392 }
393
394 return $wp_timezone;
395 }
396 }
397