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

393 lines 8.1 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 * Gets 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( $local = true ): string {
274 return $this->format( 'mysql' );
275 }
276
277 /**
278 * Consider the date is in GMT and convert to local time with
279 * gmt_offset option of WP Core.
280 *
281 * @param string $format
282 *
283 * @return int|string
284 * @since 4.0.0
285 * @deprecated 4.1.7.3
286 */
287 public function toLocal( $format = 'Y-m-d H:i:s' ) {
288 _deprecated_function( __METHOD__, '4.1.7.3' );
289 $time = $this->getTimestamp() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
290
291 if ( $format ) {
292 return date( $format, $time ); // phpcs:ignore
293 }
294
295 return $time;
296 }
297
298 /**
299 * Gets the date as an RFC 822 string. IETF RFC 2822 supercedes RFC 822 and its definition
300 * can be found at the IETF Web site.
301 *
302 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
303 *
304 * @return string
305 * @deprecated 4.1.7.3
306 */
307 public function toRFC822( $local = true ) {
308 _deprecated_function( __METHOD__, '4.1.7.3' );
309 return $this->format( DateTime::RFC2822, $local );
310 }
311
312 /**
313 * Gets the date as UNIX time stamp.
314 *
315 * @return integer The date as a UNIX timestamp.
316 * @deprecated 4.1.7.3
317 */
318 public function toUnix() {
319 _deprecated_function( __METHOD__, '4.1.7.3' );
320 //return (int) parent::format( 'U' );
321 }
322
323 /**
324 * Get timestamp of Date.
325 *
326 * @return int
327 */
328 public function getTimestamp( $local = true ): int {
329 try {
330 $date = new DateTime( $this->get_raw_date() );
331 } catch ( Throwable $e ) {
332 error_log( $e->getMessage() );
333 $date = new DateTime();
334 }
335
336 return $date->getTimestamp();
337 }
338
339 /**
340 * @deprecated 4.1.7.3
341 */
342 protected function setGMT( $local = false, $gmt = true ) {
343 _deprecated_function( __METHOD__, '4.1.7.3' );
344 /*if ( $gmt ) {
345 if ( $local == false && ! empty( self::$gmt ) ) {
346 parent::setTimezone( self::$gmt );
347 }
348 } else {
349 if ( $local == false && ! empty( $this->tz ) ) {
350 parent::setTimezone( $this->tz );
351 }
352 }*/
353 }
354
355 /**
356 * @deprecated 4.1.7.3
357 */
358 public static function getSqlNullDate() {
359 _deprecated_function( __METHOD__, '4.1.7.3' );
360 return '0000-00-00 00:00:00';
361 }
362
363 /**
364 * Add X seconds into datetime of this object.
365 *
366 * @param int $seconds
367 *
368 * @throws
369 *
370 * @since 3.3.0
371 * @deprecated 4.1.7.3
372 */
373 public function addDuration( $seconds ) {
374 _deprecated_function( __METHOD__, '4.1.7.3' );
375 $timestamp = $this->getTimestamp();
376 //parent::__construct( date( 'Y-m-d H:i:s', $timestamp + $seconds ), $this->tz ); // phpcs:ignore
377 }
378
379 /**
380 * @deprecated 4.1.7.3
381 */
382 public function getPeriod( $seconds, $local = true ) {
383 _deprecated_function( __METHOD__, '4.1.7.3' );
384 /*$timestamp = $this->getTimestamp( $local );
385
386 if ( ! is_numeric( $seconds ) ) {
387 $seconds = strtotime( $seconds ) - time();
388 }
389
390 return date( 'Y-m-d H:i:s', $timestamp + $seconds );*/
391 }
392 }
393