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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / helpers / DateTimeHelper.php
tutor / helpers Last commit date
DateTimeHelper.php 1 year ago HttpHelper.php 1 year ago QueryHelper.php 1 year ago SessionHelper.php 3 years ago ValidationHelper.php 1 year ago
DateTimeHelper.php
260 lines
1 <?php
2 /**
3 * Helper class to work with datetime.
4 *
5 * @package Tutor\Helper
6 * @author Themeum <support@themeum.com>
7 * @link https://themeum.com
8 * @since 3.0.0
9 */
10
11 namespace Tutor\Helpers;
12
13 use DateInterval;
14 use DateTime;
15 use DateTimeZone;
16
17 /**
18 * DateTimeHelper class
19 *
20 * @since 3.0.0
21 */
22 final class DateTimeHelper {
23 /**
24 * Format constants
25 */
26 const FORMAT_MYSQL = 'Y-m-d H:i:s';
27 const FORMAT_DATE_TIME = 'Y-m-d H:i:s';
28 const FORMAT_DATE = 'Y-m-d';
29 const FORMAT_TIME = 'H:i:s';
30 const FORMAT_TIMESTAMP = 'U';
31
32 /**
33 * Interval constants
34 */
35 const INTERVAL_HOUR = 'hour';
36 const INTERVAL_DAY = 'day';
37 const INTERVAL_WEEK = 'week';
38 const INTERVAL_MONTH = 'month';
39 const INTERVAL_YEAR = 'year';
40
41 /**
42 * Date
43 *
44 * @var DateTime
45 */
46 private $datetime;
47
48 /**
49 * Create an instance.
50 */
51 private static function instance() {
52 return new self();
53 }
54
55 /**
56 * Get current time.
57 *
58 * @return self
59 */
60 public static function now() {
61 $instance = self::instance();
62 $instance->datetime = new DateTime();
63 return $instance;
64 }
65
66 /**
67 * Create time from date time string or timestamp.
68 *
69 * @param string|int $datetime datetime string or timestamp.
70 * @param string|DateTimeZone $timezone timezone string or object.
71 *
72 * @return self
73 */
74 public static function create( $datetime, $timezone = null ) {
75 $instance = self::instance();
76 $instance->datetime = new DateTime(
77 $datetime,
78 $timezone instanceof DateTimeZone ? $timezone : ( $timezone ? new DateTimeZone( $timezone ) : null )
79 );
80
81 return $instance;
82 }
83
84 /**
85 * Set timezone
86 *
87 * @param string|DateTimeZone $timezone timezone string or object.
88 *
89 * @return self
90 */
91 public function set_timezone( $timezone ) {
92 $tz = is_string( $timezone ) ? new DateTimeZone( $timezone ) : $timezone;
93 $this->datetime->setTimezone( $tz );
94 return $this;
95 }
96
97 /**
98 * Add
99 *
100 * @param int $number number of interval.
101 * @param string $interval interval type (day, month, year, etc).
102 *
103 * @return self
104 */
105 public function add( $number, $interval ) {
106 $this->datetime->add( DateInterval::createFromDateString( "{$number} {$interval}" ) );
107 return $this;
108 }
109
110 /**
111 * Sub
112 *
113 * @param int $number number of interval.
114 * @param string $interval interval type (day, month, year, etc).
115 *
116 * @since 3.0.0
117 *
118 * @return self
119 */
120 public function sub( $number, $interval ) {
121 $this->datetime->sub( DateInterval::createFromDateString( "{$number} {$interval}" ) );
122 return $this;
123 }
124
125 /**
126 * Check date time is past
127 *
128 * @return boolean
129 */
130 public function is_past() {
131 return $this->datetime->getTimestamp() < time();
132 }
133
134 /**
135 * Check date time is future
136 *
137 * @return boolean
138 */
139 public function is_future() {
140 return $this->datetime->getTimestamp() > time();
141 }
142
143 /**
144 * Get timezone
145 *
146 * @return DateTimeZone
147 */
148 public function get_timezone() {
149 return $this->datetime->getTimezone();
150 }
151
152 /**
153 * Get timezone string
154 *
155 * @return string
156 */
157 public function get_timezone_string() {
158 return $this->datetime->getTimezone()->getName();
159 }
160
161 /**
162 * Format datetime ( WP i18 translation supported )
163 *
164 * @param string $format format for date. Default is mysql format.
165 * @param bool $i18_translation i18 translation support.
166 *
167 * @return string
168 */
169 public function format( $format = null, $i18_translation = true ) {
170 if ( $i18_translation ) {
171 $result = wp_date(
172 $format ? $format : self::FORMAT_MYSQL,
173 $this->to_timestamp(),
174 $this->datetime->getTimezone()
175 );
176 } else {
177 $result = $this->datetime->format( $format ? $format : self::FORMAT_MYSQL );
178 }
179
180 return $result;
181 }
182
183 /**
184 * Get readable time difference.
185 *
186 * @return string
187 */
188 public function get_readable_diff() {
189 $now = new DateTime( 'now', $this->datetime->getTimezone() );
190 // Calculate the time difference in seconds.
191 $interval = $now->getTimestamp() - $this->datetime->getTimestamp();
192
193 // Use WordPress's human_time_diff for minutes and beyond.
194 $time_diff = human_time_diff( $this->datetime->getTimestamp(), $now->getTimestamp() );
195
196 // Handle past or future tense.
197 return ( $interval > 0 )
198 /* translators: %s: time difference */
199 ? sprintf( __( '%s ago', 'tutor' ), $time_diff )
200 /* translators: %s: time difference */
201 : sprintf( __( '%s from now', 'tutor' ), $time_diff );
202 }
203
204 /**
205 * Convert to timestamp.
206 *
207 * @return int
208 */
209 public function to_timestamp() {
210 return $this->datetime->getTimestamp();
211 }
212
213 /**
214 * Covert to date time string.
215 *
216 * @return string
217 */
218 public function to_date_time_string() {
219 return $this->format( self::FORMAT_DATE_TIME, false );
220 }
221
222 /**
223 * Covert to date string.
224 *
225 * @return string
226 */
227 public function to_date_string() {
228 return $this->format( self::FORMAT_DATE, false );
229 }
230
231 /**
232 * Covert to date string.
233 *
234 * @return string
235 */
236 public function to_time_string() {
237 return $this->format( self::FORMAT_TIME, false );
238 }
239
240 /**
241 * Get GMT date to user timezone date.
242 *
243 * @since 3.0.0
244 *
245 * @param string $gmt_date gmt date time string.
246 * @param string $format format string.
247 * @param int|object $user id or object. 0 for current user (optional).
248 *
249 * @return string
250 */
251 public static function get_gmt_to_user_timezone_date( string $gmt_date, string $format = null, $user = 0 ): string {
252 $default_format = get_option( 'date_format' ) . ', ' . get_option( 'time_format' );
253 $format = is_null( $format ) ? $default_format : $format;
254
255 return self::create( $gmt_date )
256 ->set_timezone( \TUTOR\User::get_user_timezone_string( $user ) )
257 ->format( $format );
258 }
259 }
260