Passed
Push — developer ( 884b36...3fc379 )
by Radosław
32:13 queued 16:24
created

DateTime::formatRangeToDisplay()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 20
1
<?php
2
/**
3
 * Tools for datetime class.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Arkadiusz Sołek <[email protected]>
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 */
12
13
namespace App\Fields;
14
15
/**
16
 * DateTime class.
17
 */
18
class DateTime
19
{
20
	/**
21
	 * Function returns the date in user specified format.
22
	 *
23
	 * @param string $value Date time
24
	 *
25 4
	 * @return string
26
	 */
27 4
	public static function formatToDisplay($value)
28
	{
29
		if (empty($value) || '0000-00-00' === $value || '0000-00-00 00:00:00' === $value) {
30 4
			return '';
31
		}
32
		if ('now' === $value) {
33 4
			$value = null;
34
		}
35
		return (new \DateTimeField($value))->getDisplayDateTimeValue();
36
	}
37
38
	/**
39
	 * Function to get date and time value for db format.
40
	 *
41
	 * @param string $value        Date time
42
	 * @param bool   $leadingZeros
43
	 *
44
	 * @return string
45
	 */
46
	public static function formatToDb($value, $leadingZeros = false)
47
	{
48
		if ($leadingZeros) {
49
			$delim = ['/', '.'];
50
			foreach ($delim as $delimiter) {
51
				$x = strpos($value, $delimiter);
52
				if (false !== $x) {
53
					$value = str_replace($delimiter, '-', $value);
54
					break;
55
				}
56
			}
57
			[$y, $m, $d] = explode('-', $value);
58
			if (1 == \strlen($y)) {
59
				$y = '0' . $y;
60
			}
61
			if (1 == \strlen($m)) {
62
				$m = '0' . $m;
63
			}
64
			if (1 == \strlen($d)) {
65
				$d = '0' . $d;
66
			}
67
			$value = implode('-', [$y, $m, $d]);
68
			$valueList = explode(' ', $value);
69
			$dbTimeValue = $valueList[1];
70
			if (!empty($dbTimeValue) && false === strpos($dbTimeValue, ':')) {
71
				$dbTimeValue = $dbTimeValue . ':';
72
			}
73
			if (!empty($dbTimeValue) && strrpos($dbTimeValue, ':') == (\strlen($dbTimeValue) - 1)) {
74
				$dbTimeValue = $dbTimeValue . '00';
75
			}
76
			return (new \DateTimeField($valueList[0] . ' ' . $dbTimeValue))->getDBInsertDateTimeValue();
0 ignored issues
show
Bug introduced by
$valueList[0] . ' ' . $dbTimeValue of type string is incompatible with the type type expected by parameter $value of DateTimeField::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
			return (new \DateTimeField(/** @scrutinizer ignore-type */ $valueList[0] . ' ' . $dbTimeValue))->getDBInsertDateTimeValue();
Loading history...
77
		}
78
		return (new \DateTimeField($value))->getDBInsertDateTimeValue();
79
	}
80
81
	/**
82
	 * The function returns the date according to the user's settings.
83
	 *
84
	 * @param string $dateTime Date time
85
	 *
86
	 * @return string
87 3
	 */
88
	public static function formatToViewDate($dateTime)
89 3
	{
90 3
		switch (\App\User::getCurrentUserModel()->getDetail('view_date_format')) {
91
			case 'PLL_FULL':
92 3
				return '<span title="' . \Vtiger_Util_Helper::formatDateDiffInStrings($dateTime) . '">' . static::formatToDisplay($dateTime) . '</span>';
93 3
			case 'PLL_ELAPSED':
94
				return '<span title="' . static::formatToDay($dateTime) . '">' . \Vtiger_Util_Helper::formatDateDiffInStrings($dateTime) . '</span>';
95
			case 'PLL_FULL_AND_DAY':
96
				return '<span title="' . \Vtiger_Util_Helper::formatDateDiffInStrings($dateTime) . '">' . static::formatToDay($dateTime) . '</span>';
97
			default:
98
				break;
99
		}
100
		return '-';
101
	}
102
103
	/**
104
	 * Crop date if today and only return the hour.
105
	 *
106
	 * @param string $dateTime Date time
107
	 *
108
	 * @return string
109 6
	 */
110
	public static function formatToShort(string $dateTime)
111 6
	{
112 6
		if ((new \DateTime($dateTime))->format('Y-m-d') === date('Y-m-d')) {
113
			return \App\Fields\Time::formatToDisplay($dateTime);
114
		}
115
		return static::formatToDisplay($dateTime);
116
	}
117
118
	/**
119
	 * Function to parse dateTime into days.
120
	 *
121
	 * @param string $dateTime Date time
122
	 * @param bool   $allday
123
	 *
124
	 * @return string
125 3
	 */
126
	public static function formatToDay($dateTime, $allday = false)
127 3
	{
128 3
		[$formatedDate, $timeInUserFormat] = explode(' ', static::formatToDisplay($dateTime));
129 3
		$dateDay = Date::getDayFromDate($dateTime, false, true);
130 3
		if (!$allday) {
131 3
			$timeInUserFormat = explode(':', $timeInUserFormat);
132
			if (3 === \count($timeInUserFormat)) {
133
				[$hours, $minutes, $seconds] = $timeInUserFormat;
134 3
			} else {
135 3
				[$hours, $minutes] = $timeInUserFormat;
136
				$seconds = '';
137 3
			}
138 3
			$displayTime = $hours . ':' . $minutes . ' ' . $seconds;
139
			$formatedDate .= ' ' . \App\Language::translate('LBL_AT') . ' ' . $displayTime;
140 3
		}
141
		return $formatedDate . " ($dateDay)";
142
	}
143
144
	/**
145
	 * Time zone cache.
146
	 *
147
	 * @var string
148
	 */
149
	protected static $databaseTimeZone = false;
150
151
	/**
152
	 * Get system time zone.
153
	 *
154
	 * @return string
155 14
	 */
156
	public static function getTimeZone()
157 14
	{
158 1
		if (!static::$databaseTimeZone) {
159 1
			$defaultTimeZone = date_default_timezone_get();
160
			if (empty($defaultTimeZone)) {
161
				$defaultTimeZone = \App\Config::main('default_timezone');
162 1
			}
163
			static::$databaseTimeZone = $defaultTimeZone;
164 14
		}
165
		return static::$databaseTimeZone;
166
	}
167
168
	/**
169
	 * Function returning difference in format between date times.
170
	 *
171
	 * @param string $start  ex. '2017-07-10 11:45:56
172
	 * @param string $end    ex. 2017-07-30 12:08:19
173
	 * @param string $format Default %a
174
	 *
175
	 * @see https://secure.php.net/manual/en/class.dateinterval.php
176
	 * @see https://secure.php.net/manual/en/dateinterval.format.php
177
	 *
178
	 * @return int|string difference in format
179 1
	 */
180
	public static function getDiff($start, $end, $format = '%a')
181 1
	{
182 1
		$interval = (new \DateTime($start))->diff(new \DateTime($end));
183 1
		switch ($format) {
184
			case 'years':
185 1
				return $interval->format('%Y');
186
			case 'months':
187
				$years = $interval->format('%Y');
188
				$months = 0;
189
				if ($years) {
190
					$months += $years * 12;
191
				}
192 1
				return $months + $interval->format('%m');
193 1
			case 'days':
194 1
				return $interval->format('%a');
195
			case 'hours':
196
				$days = $interval->format('%a');
197
				$hours = 0;
198
				if ($days) {
199
					$hours += 24 * $days;
200
				}
201 1
				return $hours + $interval->format('%H');
202 1
			case 'minutes':
203 1
				$days = $interval->format('%a');
204 1
				$minutes = 0;
205 1
				if ($days) {
206
					$minutes += 24 * 60 * $days;
207 1
				}
208 1
				$hours = $interval->format('%H');
209 1
				if ($hours) {
210
					$minutes += 60 * $hours;
211 1
				}
212
				return $minutes + $interval->format('%i');
213
			case 'seconds':
214
				$days = $interval->format('%a');
215
				$seconds = 0;
216
				if ($days) {
217
					$seconds += 24 * 60 * 60 * $days;
218
				}
219
				$hours = $interval->format('%H');
220
				if ($hours) {
221
					$seconds += 60 * 60 * $hours;
222
				}
223
				$minutes = $interval->format('%i');
224
				if ($minutes) {
225
					$seconds += 60 * $minutes;
226
				}
227
				return $seconds + $interval->format('%s');
228
			default:
229
				break;
230
		}
231
		return $interval->format($format);
232
	}
233
234
	/**
235
	 * Function changes the datetime format to the database format without changing the time zone.
236
	 *
237
	 * @param string $value
238
	 * @param string $fromFormat
239
	 *
240
	 * @return string
241
	 */
242
	public static function sanitizeDbFormat(string $value, string $fromFormat): string
243
	{
244
		[$date, $time] = array_pad(explode(' ', $value, 2), 2, '');
245
		if (!empty($date)) {
246
			$date = \App\Fields\Date::sanitizeDbFormat($date, $fromFormat);
247
			$value = $date;
248
			if (!empty($time)) {
249
				$value .= ' ' . \App\Fields\Time::sanitizeDbFormat($time);
250
			}
251
		}
252
		return $value;
253
	}
254
255
	/**
256
	 * Convert date from database format to user format.
257
	 *
258
	 * @param array $range ['2023-06-16 23:59:59','2023-06-16 23:59:59']
259
	 *
260
	 * @return array|bool ['03.02.2018','04.02.2018']
261
	 */
262
	public static function formatRangeToDisplay($range): array
263
	{
264
		$result = [];
265
		if (\is_array($range) && !empty($range[0]) && !empty($range[1])) {
266
			$result = [
267
				static::formatToDisplay($range[0]),
268
				static::formatToDisplay($range[1]),
269
			];
270
		}
271
		return $result;
272
	}
273
}
274