PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3.1
4.4.8 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 All 139 releases
learnpress / inc / class-lp-datetime.php

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

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