PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.1
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.2.1, at inc/class-lp-datetime.php

409 lines 8.4 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 /**
13 * @var object
14 */
15 protected static $gmt;
16
17 /**
18 * @var object
19 */
20 protected static $stz;
21
22 /**
23 * @var DateTimeZone
24 */
25 protected $tz;
26
27 /**
28 * String date time.
29 *
30 * @var string $raw_date.
31 */
32 protected $raw_date = null;
33
34 protected static $def_timezone = null;
35
36 /**
37 * Constructor.
38 *
39 * @param string|int $date
40 * @param mixed $tz
41 *
42 * @throws
43 */
44 public function __construct( $date = '', $tz = null ) {
45 if ( $date instanceof LP_Datetime ) {
46 $this->raw_date = $date->get_raw_date();
47 } else {
48 $this->raw_date = is_numeric( $date ) ? gmdate( self::$format, $date ) : $date;
49 }
50
51 if ( empty( $this->raw_date ) ) {
52 $this->raw_date = current_time( 'mysql', 1 );
53 }
54
55 //date_default_timezone_set( 'UTC' );
56 //parent::__construct( $this->raw_date );
57 //$m = $this->format( 'm' );
58 }
59
60 /**
61 * Get default timezone from param and wp settings
62 *
63 * @param mixed $tz
64 *
65 * @return DateTimeZone|null|string
66 * @deprecated 4.1.7.3
67 */
68 public static function get_default_timezone( $tz ) {
69 _deprecated_function( __METHOD__, '4.1.7.3' );
70 if ( empty( self::$def_timezone ) ) {
71 if ( $tz === null ) {
72 $tz = wp_timezone();
73 } elseif ( is_string( $tz ) && $tz ) {
74 $tz = new DateTimeZone( $tz );
75 }
76 self::$def_timezone = $tz;
77 }
78
79 return self::$def_timezone;
80 }
81
82 /**
83 * Check if time is exceeded with current time
84 *
85 * using by Addon Content Drip.
86 *
87 * @since 3.0.1
88 * @version 4.0.1
89 */
90 public function is_exceeded(): bool {
91 return $this->getTimestamp() >= time();
92 }
93
94 /**
95 * Check date is null
96 *
97 * @return bool
98 */
99 public function is_null(): bool {
100 return ! $this->raw_date || $this->raw_date === '0000-00-00 00:00:00';
101 }
102
103 public function get_raw_date() {
104 return $this->raw_date;
105 }
106
107 /**
108 * @param string $name The name of the property.
109 *
110 * @return mixed
111 * @deprecated 4.1.7.3
112 */
113 public function __get( $name ) {
114 _deprecated_function( __METHOD__, '4.1.7.3' );
115 /*$value = null;
116
117 switch ( $name ) {
118 case 'daysinmonth':
119 $value = $this->format( 't', true );
120 break;
121
122 case 'dayofweek':
123 $value = $this->format( 'N', true );
124 break;
125
126 case 'dayofyear':
127 $value = $this->format( 'z', true );
128 break;
129
130 case 'isleapyear':
131 $value = (bool) $this->format( 'L', true );
132 break;
133
134 case 'day':
135 $value = $this->format( 'd', true );
136 break;
137
138 case 'hour':
139 $value = $this->format( 'H', true );
140 break;
141
142 case 'minute':
143 $value = $this->format( 'i', true );
144 break;
145
146 case 'second':
147 $value = $this->format( 's', true );
148 break;
149
150 case 'month':
151 $value = $this->format( 'm', true );
152 break;
153
154 case 'ordinal':
155 $value = $this->format( 'S', true );
156 break;
157
158 case 'week':
159 $value = $this->format( 'W', true );
160 break;
161
162 case 'year':
163 $value = $this->format( 'Y', true );
164 break;
165
166 default:
167 }
168
169 return $value;*/
170 }
171
172 /**
173 * @return string The date as a formatted string.
174 */
175 public function __toString() {
176 return $this->format( self::$format );
177 }
178
179 /**
180 * Gets the date as a formatted string.
181 *
182 * @param string $format Set i18n to return date in local time.
183 * @param boolean $local.
184 *
185 * @since 3.0.0
186 * @version 4.0.1
187 * @return string.
188 */
189 public function format( string $format = '', bool $local = true ): string {
190 $date_str = '';
191
192 if ( '0000-00-00 00:00:00' === $this->get_raw_date() ) {
193 return $date_str;
194 }
195
196 if ( empty( $format ) ) {
197 $format = get_option( 'date_format', 'Y-m-d' );
198 }
199
200 switch ( $format ) {
201 case 'i18n':
202 $date_str = learn_press_date_i18n( $this->getTimestamp() );
203 break;
204 /*case 'timestamp':
205 $date_str = $this->getTimestamp();
206 break;*/
207 case 'human':
208 $time = $this->getTimestamp();
209 $time_diff = time() - $time;
210
211 if ( $time_diff > 0 ) {
212 $date_str = sprintf( __( '%s ago', 'learnpress' ), human_time_diff( $time, time() ) );
213 }
214 break;
215 case 'mysql':
216 $date_str = gmdate( 'Y-m-d H:i:s', $this->getTimestamp() );
217 break;
218 default:
219 $date_str = gmdate( $format, $this->getTimestamp() );
220 }
221
222 if ( ! is_string( $date_str ) ) {
223 $date_str = '';
224 }
225
226 return $date_str;
227 }
228
229 /**
230 * @param boolean $hours True to return the value in hours.
231 *
232 * @return float
233 * @deprecated 4.1.7.3
234 */
235 public function getOffset( $hours = false ) {
236 _deprecated_function( __METHOD__, '4.1.7.3' );
237 return $this->tz ? (float) $hours ? ( $this->tz->getOffset( $this ) / 3600 ) : $this->tz->getOffset( $this ) : 0;
238 }
239
240 /**
241 * @param DateTimeZone $tz The new DateTimeZone object.
242 *
243 * @return void
244 * @deprecated 4.1.7.3
245 */
246 /*public function setTimezone( $tz ) {
247 _deprecated_function( __METHOD__, '4.1.7.3' );
248 $this->tz = $tz;
249
250 parent::setTimezone( $tz );
251 }*/
252
253 /**
254 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
255 *
256 * @return string
257 * @deprecated 4.1.7.3
258 */
259 public function toISO8601( $local = true ) {
260 _deprecated_function( __METHOD__, '4.1.7.3' );
261 //return $this->format( DateTime::RFC3339, $local );
262 }
263
264 /**
265 * Get the date as an SQL datetime string.
266 *
267 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
268 *
269 * @since 3.0.0
270 * @version 4.0.1
271 * @return string
272 */
273 public function toSql( bool $local = false ): string {
274 if ( $local ) {
275 return $this->toSqlLocal();
276 }
277
278 return $this->format( 'mysql' );
279 }
280
281 /**
282 * Consider the date is in GMT and convert to local time with
283 * gmt_offset option of WP Core.
284 *
285 * @param string $format
286 *
287 * @return int|string
288 * @since 4.0.0
289 * @deprecated 4.1.7.3
290 */
291 public function toLocal( $format = 'Y-m-d H:i:s' ) {
292 _deprecated_function( __METHOD__, '4.1.7.3' );
293 $time = $this->getTimestamp() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
294
295 if ( $format ) {
296 return date( $format, $time ); // phpcs:ignore
297 }
298
299 return $time;
300 }
301
302 /**
303 * Convert to format sql local time.
304 *
305 * @return string
306 * @since 4.2.1
307 */
308 private function toSqlLocal(): string {
309 $time = $this->getTimestamp() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
310
311 return gmdate( LP_Datetime::$format, $time );
312 }
313
314 /**
315 * Gets the date as an RFC 822 string. IETF RFC 2822 supercedes RFC 822 and its definition
316 * can be found at the IETF Web site.
317 *
318 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
319 *
320 * @return string
321 * @deprecated 4.1.7.3
322 */
323 public function toRFC822( $local = true ) {
324 _deprecated_function( __METHOD__, '4.1.7.3' );
325 return $this->format( DateTime::RFC2822, $local );
326 }
327
328 /**
329 * Gets the date as UNIX time stamp.
330 *
331 * @return integer The date as a UNIX timestamp.
332 * @deprecated 4.1.7.3
333 */
334 public function toUnix() {
335 _deprecated_function( __METHOD__, '4.1.7.3' );
336 //return (int) parent::format( 'U' );
337 }
338
339 /**
340 * Get timestamp of Date.
341 *
342 * @return int
343 */
344 public function getTimestamp( $local = true ): int {
345 try {
346 $date = new DateTime( $this->get_raw_date() );
347 } catch ( Throwable $e ) {
348 error_log( $e->getMessage() );
349 $date = new DateTime();
350 }
351
352 return $date->getTimestamp();
353 }
354
355 /**
356 * @deprecated 4.1.7.3
357 */
358 protected function setGMT( $local = false, $gmt = true ) {
359 _deprecated_function( __METHOD__, '4.1.7.3' );
360 /*if ( $gmt ) {
361 if ( $local == false && ! empty( self::$gmt ) ) {
362 parent::setTimezone( self::$gmt );
363 }
364 } else {
365 if ( $local == false && ! empty( $this->tz ) ) {
366 parent::setTimezone( $this->tz );
367 }
368 }*/
369 }
370
371 /**
372 * @deprecated 4.1.7.3
373 */
374 public static function getSqlNullDate() {
375 _deprecated_function( __METHOD__, '4.1.7.3' );
376 return '0000-00-00 00:00:00';
377 }
378
379 /**
380 * Add X seconds into datetime of this object.
381 *
382 * @param int $seconds
383 *
384 * @throws
385 *
386 * @since 3.3.0
387 * @deprecated 4.1.7.3
388 */
389 public function addDuration( $seconds ) {
390 _deprecated_function( __METHOD__, '4.1.7.3' );
391 $timestamp = $this->getTimestamp();
392 //parent::__construct( date( 'Y-m-d H:i:s', $timestamp + $seconds ), $this->tz ); // phpcs:ignore
393 }
394
395 /**
396 * @deprecated 4.1.7.3
397 */
398 public function getPeriod( $seconds, $local = true ) {
399 _deprecated_function( __METHOD__, '4.1.7.3' );
400 /*$timestamp = $this->getTimestamp( $local );
401
402 if ( ! is_numeric( $seconds ) ) {
403 $seconds = strtotime( $seconds ) - time();
404 }
405
406 return date( 'Y-m-d H:i:s', $timestamp + $seconds );*/
407 }
408 }
409