PluginProbe ʕ •ᴥ•ʔ
پارسی دیت – Parsi Date / trunk
پارسی دیت – Parsi Date vtrunk
6.2 6.1 5.1.6 5.1.7 5.1.8 5.1.8.2 6.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.0.0-alpha 2.1 2.1.1 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0.1 2.3.0.2 2.3.1 2.3.2 2.3.3 2.3.4 3.0.1 3.0.2 3.0.3 4.0.0 4.0.1 4.0.2 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5
wp-parsidate / inc / Helper / Date.php
wp-parsidate / inc / Helper Last commit date
Assets.php 6 hours ago Cache.php 3 weeks ago Date.php 6 hours ago Debug.php 5 hours ago FeedReader.php 6 hours ago HTML.php 6 hours ago Helper.php 3 weeks ago JSON.php 3 weeks ago Nonce.php 3 weeks ago Notice.php 6 hours ago Number.php 3 weeks ago NumberConverter.php 6 hours ago Param.php 3 weeks ago Sanitizing.php 6 hours ago Strip.php 3 weeks ago Templates.php 3 weeks ago User.php 3 weeks ago Validating.php 3 weeks ago WooCommerce.php 3 weeks ago WordPress.php 6 hours ago
Date.php
146 lines
1 <?php
2
3 namespace WPParsidate\Helper;
4
5 use DateTime;
6 use DateTimeZone;
7
8 class Date {
9 /**
10 * Get time zone offset base on timezone input
11 *
12 * @param DateTimeZone|string $timezone
13 *
14 * @return float|int
15 * @throws \DateInvalidTimeZoneException
16 */
17 public static function getTimeZoneOffset( $timezone ) {
18 $dtz = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone( $timezone );
19 $datetime = new DateTime( 'now', $dtz );
20
21 return $dtz->getOffset( $datetime ) / HOUR_IN_SECONDS;
22 }
23
24 /**
25 * Will return an offset using the WordPress timezone set by the user
26 * Example return values: -04:00, +00:00, or +5:45
27 *
28 * @source https://gist.github.com/andrewjmead/9aef80a495dc36221ff84ddfb3ac3181
29 */
30 public static function getLocalOffset(): string {
31 // Start with the offset such as -4, 0, or 5.75
32 $offset_number = (float) get_option( 'gmt_offset' );
33
34 // Build a string to represent the offset such as -04:00, +00:00, or +5:45
35 $result = '';
36
37 // Start with either - or +
38 $result .= $offset_number < 0 ? '-' : '+';
39
40 $whole_part = abs( $offset_number );
41 $hour_part = floor( $whole_part );
42 $minute_part = $whole_part - $hour_part;
43
44 $hours = strval( $hour_part );
45 $minutes = strval( $minute_part * 60 );
46
47 // Add hour part to result
48 $result .= str_pad( $hours, 2, '0', STR_PAD_LEFT );
49
50 // Add separator
51 $result .= ':';
52
53 // Add minute part to result
54 $result .= str_pad( $minutes, 2, '0', STR_PAD_LEFT );
55
56 return $result;
57 }
58
59 /**
60 * Change date format
61 *
62 * @param string $date Date string
63 * @param string $dateFormat Input date format
64 * @param string $returnFormat Output date format
65 *
66 * @return string Date with output format
67 */
68 public static function changeDateFormat( string $date, string $dateFormat, string $returnFormat ): string {
69 return DateTime::createFromFormat( $dateFormat, $date )->format( $returnFormat );
70 }
71
72 /**
73 * Determines is gregorian/shamsi date string
74 *
75 * @param string $dateString Date string
76 * @param string $format Date format
77 *
78 * @return array
79 */
80 public static function isDateString( string $dateString, string $format = 'Y-m-d\TH:i:sP' ): array {
81 $default = [
82 'status' => false,
83 'type' => null,
84 'value' => ''
85 ];
86
87 $dateString = Number::toEnglish( $dateString );
88 $dateParts = date_parse_from_format( $format, $dateString );
89 if ( $dateParts['error_count'] > 0 || $dateParts['warning_count'] > 0 ) {
90 return $default;
91 }
92
93 $year = $dateParts['year'];
94 $month = $dateParts['month'];
95 $day = $dateParts['day'];
96
97 if ( $year > 1900 ) {
98 if ( checkdate( $month, $day, $year ) ) {
99 return [
100 'status' => true,
101 'type' => 'gregorian',
102 'value' => $dateString
103 ];
104 }
105 } elseif ( $year < 1500 ) {
106 if ( $month >= 1 && $month <= 12 && $day >= 1 && $day <= 31 ) {
107 if ( $month > 6 && $day > 30 ) {
108 return $default;
109 }
110
111 return [
112 'status' => true,
113 'type' => 'jalali',
114 'value' => $dateString
115 ];
116 }
117 }
118
119 return $default;
120 }
121
122 /**
123 * Determines is time string
124 *
125 * @param mixed $time Time string
126 * @param string $seconds Seconds string value
127 *
128 * @return false|string Return time string if is time, Otherwise false
129 */
130 public static function isTimeString( $time, string $seconds = '00' ) {
131 if ( ! is_string( $time ) ) {
132 return false;
133 }
134
135 if ( preg_match( '/^(?:2[0-3]|[01][0-9]):[0-5][0-9](?::[0-5][0-9])?$/', $time ) ) {
136 if ( substr_count( $time, ':' ) === 1 ) {
137 $time .= ':' . $seconds;
138 }
139
140 return $time;
141 }
142
143 return false;
144 }
145 }
146