Completed
Push — master ( 16ddfb...0ea243 )
by Henry
09:18
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 Redaxscript\Model;
7
use function date_default_timezone_get;
8
9
/**
10
 * parent class to handle the date time
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Date
16
 * @author Henry Ruhs
17
 */
18
19
class Dater
20
{
21
	/**
22
	 * time zone
23
	 *
24
	 * @var DateTimeZone
25
	 */
26
27
	protected $_timeZone;
28
29
	/**
30
	 * date time
31
	 *
32
	 * @var DateTime
33
	 */
34
35
	protected $_dateTime;
36
37
	/**
38
	 * init the class
39
	 *
40
	 * @since 4.0.0
41
	 *
42
	 * @param int $date timestamp of the date
43
	 */
44
45 2
	public function init(int $date = null) : void
46
	{
47 2
		$zone = null;
48 2
		if (Db::getStatus() === 2)
49
		{
50 2
			$settingModel = new Model\Setting();
51 2
			$zone = $settingModel->get('zone');
52
		}
53 2
		$this->_timeZone = new DateTimeZone($zone ? : date_default_timezone_get());
54 2
		$this->_dateTime = new DateTime();
55 2
		$this->_dateTime->setTimezone($this->_timeZone);
56 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...
57
		{
58 1
			$this->_dateTime->setTimestamp($date);
59
		}
60 2
	}
61
62
	/**
63
	 * get the time zone
64
	 *
65
	 * @since 4.0.0
66
	 *
67
	 * @return DateTimeZone
68
	 */
69
70 1
	public function getTimeZone() : DateTimeZone
71
	{
72 1
		return $this->_timeZone;
73
	}
74
75
	/**
76
	 * get the date time
77
	 *
78
	 * @since 4.0.0
79
	 *
80
	 * @return DateTime
81
	 */
82
83 2
	public function getDateTime() : DateTime
84
	{
85 2
		return $this->_dateTime;
86
	}
87
88
	/**
89
	 * format to time
90
	 *
91
	 * @since 4.0.0
92
	 *
93
	 * @return string
94
	 */
95
96 1
	public function formatTime() : string
97
	{
98 1
		$settingModel = new Model\Setting();
99 1
		return $this->getDateTime()->format($settingModel->get('time'));
100
	}
101
102
	/**
103
	 * format to date
104
	 *
105
	 * @since 4.0.0
106
	 *
107
	 * @return string
108
	 */
109
110 1
	public function formatDate() : string
111
	{
112 1
		$settingModel = new Model\Setting();
113 1
		return $this->getDateTime()->format($settingModel->get('date'));
114
	}
115
116
	/**
117
	 * format to field
118
	 *
119
	 * @since 4.0.0
120
	 *
121
	 * @return string
122
	 */
123
124 1
	public function formatField() : string
125
	{
126 1
		return $this->getDateTime()->format('Y-m-d\TH:i');
127
	}
128
}
129