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

362 lines 7.7 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
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 protected $raw_date = null;
28
29 protected static $def_timezone = null;
30
31 /**
32 * Constructor.
33 *
34 * @param string $date
35 * @param mixed $tz
36 *
37 * @throws
38 */
39 public function __construct( $date = '', $tz = null ) {
40 if ( empty( self::$gmt ) || empty( self::$stz ) ) {
41 self::$gmt = new DateTimeZone( 'GMT' );
42 self::$stz = new DateTimeZone( @date_default_timezone_get() ); // phpcs:ignore
43 }
44
45 if ( $date instanceof LP_Datetime ) {
46 $this->raw_date = $date->get_raw_date();
47 } else {
48 $this->raw_date = is_numeric( $date ) ? gmdate( 'Y-m-d H:i:s', $date ) : $date;
49 }
50
51 if ( empty( $date ) ) {
52 $date = current_time( 'mysql' );
53 }
54
55 if ( ! ( $tz instanceof DateTimeZone ) ) {
56 $tz = self::get_default_timezone( $tz );
57 }
58
59 if ( ! $tz ) {
60 $tz = null;
61 }
62
63 date_default_timezone_set( 'UTC' );
64
65 parent::__construct( $this->raw_date, $tz );
66
67 date_default_timezone_set( self::$stz->getName() ); // phpcs:ignore
68
69 $this->tz = $tz;
70 }
71
72 /**
73 * Get default timezone from param and wp settings
74 *
75 * @param mixed $tz
76 *
77 * @return DateTimeZone|null|string
78 */
79 public static function get_default_timezone( $tz ) {
80 if ( empty( self::$def_timezone ) ) {
81 if ( $tz === null ) {
82 $tz = wp_timezone();
83 } elseif ( is_string( $tz ) && $tz ) {
84 $tz = new DateTimeZone( $tz );
85 }
86 self::$def_timezone = $tz;
87 }
88
89 return self::$def_timezone;
90 }
91
92 /**
93 * Check if time is exceeded with current time
94 */
95 public function is_exceeded( $interval = 0 ) {
96 return $this->getTimestamp() >= current_time( 'timestamp' ) + $interval; // phpcs:ignore
97 }
98
99 public function is_null() {
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 */
112 public function __get( $name ) {
113 $value = null;
114
115 switch ( $name ) {
116 case 'daysinmonth':
117 $value = $this->format( 't', true );
118 break;
119
120 case 'dayofweek':
121 $value = $this->format( 'N', true );
122 break;
123
124 case 'dayofyear':
125 $value = $this->format( 'z', true );
126 break;
127
128 case 'isleapyear':
129 $value = (bool) $this->format( 'L', true );
130 break;
131
132 case 'day':
133 $value = $this->format( 'd', true );
134 break;
135
136 case 'hour':
137 $value = $this->format( 'H', true );
138 break;
139
140 case 'minute':
141 $value = $this->format( 'i', true );
142 break;
143
144 case 'second':
145 $value = $this->format( 's', true );
146 break;
147
148 case 'month':
149 $value = $this->format( 'm', true );
150 break;
151
152 case 'ordinal':
153 $value = $this->format( 'S', true );
154 break;
155
156 case 'week':
157 $value = $this->format( 'W', true );
158 break;
159
160 case 'year':
161 $value = $this->format( 'Y', true );
162 break;
163
164 default:
165 }
166
167 return $value;
168 }
169
170 /**
171 * @return string The date as a formatted string.
172 */
173 public function __toString() {
174 return (string) $this->format( self::$format, true );
175 }
176
177 /**
178 * Gets the date as a formatted string.
179 *
180 * @param string $format The date format specification string (see {@link PHP_MANUAL#date})
181 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
182 *
183 * @return string The date string in the specified format format.
184 */
185 public function format( $format = '', $local = true ) {
186 if ( '0000-00-00 00:00:00' === $this->raw_date ) {
187 return '';
188 }
189
190 if ( empty( $format ) ) {
191 $format = 'mysql';
192 }
193
194 $return = false;
195
196 switch ( $format ) {
197 case 'i18n':
198 $return = learn_press_date_i18n( $this->getTimestamp( $local ) );
199 break;
200 case 'timestamp':
201 $return = $this->getTimestamp( $local );
202 break;
203 case 'human':
204 $time = $this->getTimestamp( true );// mysql2date( 'G', $date->format('Y-m-d H:i:s') );
205 $time1 = $this->getTimestamp( false );// mysql2date( 'G', $date->format('Y-m-d H:i:s') );
206 $time_diff = ( time() ) - $time1;
207
208 if ( $time_diff > 0 ) {
209 $return = sprintf( __( '%s ago', 'learnpress' ), human_time_diff( $time1, time() ) );
210 }
211 break;
212 case 'mysql':
213 $return = $this->format( 'Y-m-d H:i:s', $local );
214 break;
215 default:
216 if ( ! $local && ! empty( self::$gmt ) ) {
217 parent::setTimezone( self::$gmt );
218 }
219
220 $return = parent::format( $format );
221
222 if ( ! $local && ! empty( $this->tz ) ) {
223 parent::setTimezone( $this->tz );
224 }
225 }
226
227 return $return;
228 }
229
230 /**
231 * @param boolean $hours True to return the value in hours.
232 *
233 * @return float
234 */
235 public function getOffset( $hours = false ) {
236 return $this->tz ? (float) $hours ? ( $this->tz->getOffset( $this ) / 3600 ) : $this->tz->getOffset( $this ) : 0;
237 }
238
239 /**
240 * @param DateTimeZone $tz The new DateTimeZone object.
241 *
242 * @return void
243 */
244 public function setTimezone( $tz ) {
245 $this->tz = $tz;
246
247 parent::setTimezone( $tz );
248 }
249
250 /**
251 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
252 *
253 * @return string
254 */
255 public function toISO8601( $local = true ) {
256 return $this->format( DateTime::RFC3339, $local );
257 }
258
259 /**
260 * Gets the date as an SQL datetime string.
261 *
262 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
263 *
264 * @return string
265 */
266 public function toSql( $local = true ) {
267 return $this->format( 'Y-m-d H:i:s', $local );
268 }
269
270 /**
271 * Consider the date is in GMT and convert to local time with
272 * gmt_offset option of WP Core.
273 *
274 * @param string $format
275 *
276 * @return int|string
277 * @since 4.0.0
278 */
279 public function toLocal( $format = 'Y-m-d H:i:s' ) {
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 */
306 public function toUnix() {
307 return (int) parent::format( 'U' );
308 }
309
310 public function getTimestamp( $local = true ) {
311 $this->setGMT( $local );
312 $timestamp = parent::getTimestamp();
313 $this->setGMT( $local, false );
314
315 if ( $local ) {
316 $timestamp += $this->getOffset();
317 }
318
319 return $timestamp;
320 }
321
322 protected function setGMT( $local = false, $gmt = true ) {
323 if ( $gmt ) {
324 if ( $local == false && ! empty( self::$gmt ) ) {
325 parent::setTimezone( self::$gmt );
326 }
327 } else {
328 if ( $local == false && ! empty( $this->tz ) ) {
329 parent::setTimezone( $this->tz );
330 }
331 }
332 }
333
334 public static function getSqlNullDate() {
335 return '0000-00-00 00:00:00';
336 }
337
338 /**
339 * Add X seconds into datetime of this object.
340 *
341 * @param int $seconds
342 *
343 * @throws
344 *
345 * @since 3.3.0
346 */
347 public function addDuration( $seconds ) {
348 $timestamp = $this->getTimestamp();
349 parent::__construct( date( 'Y-m-d H:i:s', $timestamp + $seconds ), $this->tz ); // phpcs:ignore
350 }
351
352 public function getPeriod( $seconds, $local = true ) {
353 $timestamp = $this->getTimestamp( $local );
354
355 if ( ! is_numeric( $seconds ) ) {
356 $seconds = strtotime( $seconds ) - time();
357 }
358
359 return date( 'Y-m-d H:i:s', $timestamp + $seconds ); // phpcs:ignore
360 }
361 }
362