Completed
Push — master ( 0947c3...b8ee92 )
by Henry
11:06 queued 03:27
created

Dater::formatDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Redaxscript;
3
4
use DateTime;
5
use DateTimeZone;
6
use Exception;
7
use Redaxscript\Model;
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 object
25
	 */
26
27
	protected $_timeZone;
28
29
	/**
30
	 * date time
31
	 *
32
	 * @var object
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
	public function init(int $date = null)
46
	{
47
		$this->_dateTime = new DateTime();
48
		try
49
		{
50
			$settingModel = new Model\Setting();
51
			$this->_timeZone = new DateTimeZone($settingModel->get('zone'));
52
		}
53
		catch (Exception $exception)
54
		{
55
			$this->_timeZone = new DateTimeZone(date_default_timezone_get());
56
		}
57
		$this->_dateTime->setTimezone($this->_timeZone);
58
		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...
59
		{
60
			$this->_dateTime->setTimestamp($date);
61
		}
62
	}
63
64
	/**
65
	 * get the time zone
66
	 *
67
	 * @since 4.0.0
68
	 *
69
	 * @return object
70
	 */
71
72
	public function getTimeZone()
73
	{
74
		return $this->_timeZone;
75
	}
76
77
	/**
78
	 * get the date time
79
	 *
80
	 * @since 4.0.0
81
	 *
82
	 * @return object
83
	 */
84
85
	public function getDateTime()
86
	{
87
		return $this->_dateTime;
88
	}
89
90
	/**
91
	 * format to time
92
	 *
93
	 * @since 4.0.0
94
	 *
95
	 * @return string
96
	 */
97
98
	public function formatTime()
99
	{
100
		$settingModel = new Model\Setting();
101
		return $this->getDateTime()->format($settingModel->get('time'));
102
	}
103
104
	/**
105
	 * format to date
106
	 *
107
	 * @since 4.0.0
108
	 *
109
	 * @return string
110
	 */
111
112
	public function formatDate()
113
	{
114
		$settingModel = new Model\Setting();
115
		return $this->getDateTime()->format($settingModel->get('date'));
116
	}
117
118
	/**
119
	 * format to field
120
	 *
121
	 * @since 4.0.0
122
	 *
123
	 * @return string
124
	 */
125
126
	public function formatField()
127
	{
128
		return $this->getDateTime()->format('Y-m-d\TH:i');
129
	}
130
}
131