| 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 |
/** |
| 261 |
* Convert user timezone date time to GMT date time |
| 262 |
* |
| 263 |
* @since 3.0.0 |
| 264 |
* |
| 265 |
* @param string $datetime date time string. |
| 266 |
* @param string|null $timezone user' timezone string, default is WordPress timezone. |
| 267 |
* |
| 268 |
* @return string GMT date time in MySQL format |
| 269 |
*/ |
| 270 |
public static function get_user_time_to_gmt_time( string $datetime, $timezone = null ): string { |
| 271 |
$timezone = $timezone ? $timezone : wp_timezone_string(); |
| 272 |
|
| 273 |
return self::create( $datetime, $timezone ) |
| 274 |
->set_timezone( 'UTC' ) |
| 275 |
->format( self::FORMAT_MYSQL ); |
| 276 |
} |
| 277 |
} |
| 278 |
|