Completed
Push — master ( 0d72ed...db6a9f )
by Henry
14:21 queued 04:57
created

includes/Dater.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript;
3
4
use DateTime;
5
use DateTimeZone;
6
use function date_default_timezone_get;
7
8
/**
9
 * parent class to handle the date time
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Date
15
 * @author Henry Ruhs
16
 */
17
18
class Dater
19
{
20
	/**
21
	 * time zone
22
	 *
23
	 * @var DateTimeZone
24
	 */
25
26
	protected $_timeZone;
27
28
	/**
29
	 * date time
30
	 *
31
	 * @var DateTime
32
	 */
33
34
	protected $_dateTime;
35
36
	/**
37
	 * init the class
38
	 *
39
	 * @since 4.0.0
40
	 *
41
	 * @param int $date timestamp of the date
42
	 */
43
44 2
	public function init(int $date = null) : void
45
	{
46 2
		$zone = null;
47 2
		if (Db::getStatus() === 2)
48
		{
49 2
			$settingModel = new Model\Setting();
50 2
			$zone = $settingModel->get('zone');
51
		}
52 2
		$this->_timeZone = new DateTimeZone($zone ? : date_default_timezone_get());
53 2
		$this->_dateTime = new DateTime();
54 2
		$this->_dateTime->setTimezone($this->_timeZone);
55 2
		if ($date)
0 ignored issues
show
Bug Best Practice introduced by
The expression $date of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
56
		{
57 1
			$this->_dateTime->setTimestamp($date);
58
		}
59 2
	}
60
61
	/**
62
	 * get the time zone
63
	 *
64
	 * @since 4.0.0
65
	 *
66
	 * @return DateTimeZone
67
	 */
68
69 1
	public function getTimeZone() : DateTimeZone
70
	{
71 1
		return $this->_timeZone;
72
	}
73
74
	/**
75
	 * get the date time
76
	 *
77
	 * @since 4.0.0
78
	 *
79
	 * @return DateTime
80
	 */
81
82 2
	public function getDateTime() : DateTime
83
	{
84 2
		return $this->_dateTime;
85
	}
86
87
	/**
88
	 * format to time
89
	 *
90
	 * @since 4.0.0
91
	 *
92
	 * @return string
93
	 */
94
95 1
	public function formatTime() : string
96
	{
97 1
		$settingModel = new Model\Setting();
98 1
		return $this->getDateTime()->format($settingModel->get('time'));
99
	}
100
101
	/**
102
	 * format to date
103
	 *
104
	 * @since 4.0.0
105
	 *
106
	 * @return string
107
	 */
108
109 1
	public function formatDate() : string
110
	{
111 1
		$settingModel = new Model\Setting();
112 1
		return $this->getDateTime()->format($settingModel->get('date'));
113
	}
114
115
	/**
116
	 * format to field
117
	 *
118
	 * @since 4.0.0
119
	 *
120
	 * @return string
121
	 */
122
123 1
	public function formatField() : string
124
	{
125 1
		return $this->getDateTime()->format('Y-m-d\TH:i');
126
	}
127
}
128