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 / Helpers / LPDateTime.php

LPDateTime.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.4, at inc/Helpers/LPDateTime.php

276 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LearnPress\Helpers;
4
5 use DateTime;
6 use DateTimeZone;
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Class DateTime
12 *
13 * @since 4.4.2
14 * @version 1.0.0
15 */
16 class LPDateTime {
17 const FORMAT_MYSQL = 'Y-m-d H:i:s';
18 /**
19 * Format date by config WP.
20 */
21 const FORMAT_I18N_DATE = 'i18n_date';
22 /**
23 * Format date time by config WP.
24 */
25 const FORMAT_I18N_DATE_TIME = 'i18n_date_time';
26 /**
27 * Format date time Human.
28 */
29 const FORMAT_HUMAN = 'human';
30 const FORMAT_HUMAN_TWO = 'human_two';
31 /**
32 * String date time.
33 *
34 * @var string $raw_date .
35 */
36 protected $raw_date = '';
37
38 /**
39 * Constructor.
40 *
41 * @param string $date_gmt Date must be in GMT (UTC+0).
42 */
43 public function __construct( string $date_gmt = '' ) {
44 $time = strtotime( $date_gmt );
45 if ( empty( $date_gmt ) ) {
46 $time = time();
47 }
48
49 $this->raw_date = (string) gmdate( self::FORMAT_MYSQL, $time );
50 }
51
52 /**
53 * Get raw date.
54 *
55 * @return string
56 */
57 public function get_raw_date(): string {
58 return $this->raw_date;
59 }
60
61 /**
62 * Get timestamp of Date.
63 *
64 * @return int
65 */
66 public function get_timestamp(): int {
67 return strtotime( $this->get_raw_date() );
68 }
69
70 /**
71 * Format the date/time into a string.
72 *
73 * - $type_convert = 'keep' preserves the GMT/UTC time.
74 * - $type_convert = 'gmt_to_local' converts to the site's local timezone.
75 *
76 * @param string $format Output format (MYSQL, I18N_DATE, I18N_DATE_TIME, HUMAN, or a custom format string).
77 * @param string $type_convert Timezone conversion mode: keep|gmt_to_local.
78 * @param bool $return_has_timezone Whether to append the timezone name to the output.
79 *
80 * @return string The formatted date/time string.
81 */
82 public function format( string $format = '', string $type_convert = 'keep', bool $return_has_timezone = false ): string {
83 $string = '';
84 $option_date_format = get_option( 'date_format' );
85 $option_time_format = get_option( 'time_format' );
86 $timestamp = $this->get_timestamp();
87 $timezone = 'gmt_to_local' === $type_convert ? wp_timezone() : new DateTimeZone( 'UTC' );
88
89 switch ( $format ) {
90 case self::FORMAT_MYSQL:
91 $string = wp_date( self::FORMAT_MYSQL, $timestamp, $timezone );
92 break;
93 case self::FORMAT_I18N_DATE:
94 $string = wp_date( $option_date_format, $timestamp, $timezone );
95 break;
96 case self::FORMAT_I18N_DATE_TIME:
97 $string = wp_date( $option_date_format . ' ' . $option_time_format, $timestamp, $timezone );
98 break;
99 case self::FORMAT_HUMAN:
100 $string = sprintf( __( '%s ago', 'learnpress' ), human_time_diff( $timestamp, time() ) );
101 break;
102 default:
103 $string = wp_date( $format, $timestamp, $timezone );
104 break;
105 }
106
107 if ( $return_has_timezone ) {
108 $string .= ' ' . $timezone->getName();
109 }
110
111 return $string;
112 }
113
114 /**
115 * Display date human time diff.
116 * 1. Show number days, hours if >= 1 days
117 * 2. Show number hours, seconds if >= 1 hours
118 * 3. Show number seconds if < 1 hours
119 * Clone from LP_Datetime::format_human_time_diff since 4.0.3
120 *
121 * @param DateTime $date_start
122 * @param DateTime $date_end
123 *
124 * @return string
125 * @since 4.4.2
126 * @version 1.0.0
127 */
128 public static function format_human_time_diff( DateTime $date_start, DateTime $date_end ): string {
129 $diff = $date_end->diff( $date_start );
130 $week = floor( $diff->d / 7 );
131
132 $i18n_year = self::get_string_plural_duration( $diff->y, 'year' );
133 $i18n_month = self::get_string_plural_duration( $diff->m, 'month' );
134 $i18n_week = self::get_string_plural_duration( $week, 'week' );
135 $i18n_day = self::get_string_plural_duration( $diff->d, 'day' );
136 $i18n_hour = self::get_string_plural_duration( $diff->h, 'hour' );
137 $i18n_minute = self::get_string_plural_duration( $diff->i, 'minute' );
138 $i18n_second = self::get_string_plural_duration( $diff->s, 'second' );
139
140 $format_date = '';
141 $string = array(
142 'y' => '%y years',
143 'm' => '%m months',
144 'w' => '', // object don't have week, only add to custom week format
145 'd' => '%d days, %h hours',
146 'h' => '%h hours, %i minutes',
147 'i' => '%i minutes, %s seconds',
148 's' => $i18n_second,
149 );
150
151 foreach ( $string as $k => $v ) {
152 if ( isset( $diff->{$k} ) && $diff->{$k} > 0 ) {
153 switch ( $k ) {
154 case 'y':
155 $format_date = sprintf(
156 '%1$s%2$s',
157 $i18n_year,
158 $diff->m > 0 ? ', ' . $i18n_month : ''
159 );
160 break;
161 case 'm':
162 $format_date = sprintf(
163 '%1$s%2$s',
164 $i18n_month,
165 $diff->d > 0 ? ', ' . $i18n_day : ''
166 );
167 break;
168 case 'd':
169 $format_date = sprintf(
170 '%1$s%2$s',
171 $i18n_day,
172 $diff->h > 0 ? ', ' . $i18n_hour : ''
173 );
174 break;
175 case 'h':
176 $format_date = sprintf(
177 '%1$s%2$s',
178 $i18n_hour,
179 $diff->i > 0 ? ', ' . $i18n_minute : ''
180 );
181 break;
182 case 'i':
183 $format_date = sprintf(
184 '%1$s%2$s',
185 $i18n_minute,
186 $diff->s > 0 ? ', ' . $i18n_second : ''
187 );
188 break;
189 default:
190 $format_date = $v;
191 break;
192 }
193 break;
194 } elseif ( 'w' === $k && $week > 0 ) {
195 $day_remain = $diff->d - $week * 7;
196 $format_date = sprintf(
197 '%1$s%2$s',
198 $i18n_week,
199 $day_remain > 0 ? ', ' . self::get_string_plural_duration( $day_remain, 'day' ) : ''
200 );
201 break;
202 }
203 }
204
205 return apply_filters(
206 'learn-press/datetime/format_human_time_diff',
207 $format_date,
208 $diff,
209 $date_start,
210 $date_end
211 );
212 }
213
214 /**
215 * Get string plural duration.
216 * Clone from LP_Datetime::get_string_plural_duration since 4.2.3.5
217 *
218 * @param int $duration_number
219 * @param string $duration_type
220 *
221 * @return string
222 * @version 1.0.0
223 * @since 4.4.2
224 */
225 public static function get_string_plural_duration( int $duration_number, string $duration_type = '' ): string {
226 switch ( strtolower( $duration_type ) ) {
227 case 'second':
228 $duration_str = sprintf(
229 _n( '%s Second', '%s Seconds', $duration_number, 'learnpress' ),
230 $duration_number
231 );
232 break;
233 case 'minute':
234 $duration_str = sprintf(
235 _n( '%s Minute', '%s Minutes', $duration_number, 'learnpress' ),
236 $duration_number
237 );
238 break;
239 case 'hour':
240 $duration_str = sprintf(
241 _n( '%s Hour', '%s Hours', $duration_number, 'learnpress' ),
242 $duration_number
243 );
244 break;
245 case 'day':
246 $duration_str = sprintf(
247 _n( '%s Day', '%s Days', $duration_number, 'learnpress' ),
248 $duration_number
249 );
250 break;
251 case 'week':
252 $duration_str = sprintf(
253 _n( '%s Week', '%s Weeks', $duration_number, 'learnpress' ),
254 $duration_number
255 );
256 break;
257 case 'month':
258 $duration_str = sprintf(
259 _n( '%s Month', '%s Months', $duration_number, 'learnpress' ),
260 $duration_number
261 );
262 break;
263 case 'year':
264 $duration_str = sprintf(
265 _n( '%s Year', '%s Years', $duration_number, 'learnpress' ),
266 $duration_number
267 );
268 break;
269 default:
270 $duration_str = $duration_number . ' ' . $duration_type;
271 }
272
273 return apply_filters( 'learn-press/i18n/plural_duration', $duration_str, $duration_number, $duration_type );
274 }
275 }
276